BaseSettings Singleton option?

See original GitHub issue

I’ve noticed a pattern come up in several of our projects and thought maybe (but I’m really not sure) pydantic could ship support for it.

# config.py
from functools import lru_cache
from pydantic import BaseSettings


class Config(BaseSettings):
    a: str
    b: str
    c: str

@lru_cache()
def read() -> Config:
    return Config()
# a.py
from . import config
conf = config.read
# b.py
from . import config
conf = config.read

Would it be a good library addition if we could do something like:

# config.py
from functools import lru_cache
from pydantic import BaseSettings


class Config(BaseSettings):
    a: str
    b: str
    c: str

    class Config:
        singleton = True
# a.py
from . import Config
conf = Config()
# b.py
from . import config
conf = Config()

I definitely appreciate that pydantic should not try to take care of application-level features like dependency injection and object caching. However, for the special case of BaseSettings this just seems like a practical feature. Thoughts?

Maybe there is already a more general solution here that is readily available that i missed, allowing e.g.:

# config.py
from pydantic import BaseSettings

@singleton # exists?
class Config(BaseSettings):
    a: str
    b: str
    c: str

But I haven’t come across that yet. And if such a thing is from another third party lib. I still feel then that there is value in pydantic shipping an out of box solution, if its what users would want most of the time.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
samuelcolvincommented, Jun 11, 2019

I completely agree settings should be a singleton, however I’ve never had any problem creating it as a singleton which is then used throughout the application.

This is probably because most applications I build are either based on aiohttp or use aiohttp’s approach of a single “app” context.

See for example here. This approach also works well for testing where you generally want a very different settings instance which can easily be changed on a per-test basis without reloading modules.

My suspicion is that you’ve made design mistakes if you find yourself trying to initialise settings more than once. Probably best you fix the root mistake or take care of the workaround outside pydantic.

1reaction
tl24commented, Oct 31, 2022

I usually put these in a factory function with lru_cache

from functools import lru_cache

@lru_cache(maxsize=1)
def get_settings():
   return AppSettings()
Read more comments on GitHub >

github_iconTop Results From Across the Web

BaseSettings Singleton option? #586 - pydantic ... - GitHub
I have personally dealt with this by just using a module-level instance. My settings file looks like this: # config.py from pydantic import ......
Read more >
tiangolo/fastapi - Gitter
I use that json data to construct my Config(BaseSettings) . My config is module-level cached (singleton) so I just import that module, it...
Read more >
Settings and Environment Variables - FastAPI
To set multiple env vars for a single command just separate them with a space, and put them all before the command. And...
Read more >
Settings management - pydantic
One of pydantic's most useful applications is settings management. If you create a model that inherits from BaseSettings , the model initialiser will...
Read more >
Configuration provider - Dependency Injector
Configuration provides configuration options to the other providers. ... providers from pydantic import BaseSettings, Field # Emulate environment variables ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found