-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add extra settings sources to BaseSettings #2106
Comments
Thank you so much for this issue (and the PR that just got merged! 🎉) I wanted to ask the same almost a year ago, when I started working on a POC to extend Pydantic settings with Hashicorp Vault (see https://github.com/nymous/pydantic-vault). I ended up overloading the I still have a few questions: now that we can extend how Pydantic loads variables, what would be the best way to write a reusable provider (to be published on PyPI)? Should I just export a Anyway, thanks for opening the issue that I never took the time for, thanks for working on the PR, and thanks for Pydantic for being awesome ❤️ |
closed by #2107 |
Checks
Feature Request
As of today, Pydantic supports loading settings from multiples sources using BaseSettings:
This feature is great and saves me a lot of time during development (using .env) and in some production environments using the docker secrets feature 🙂🙏
However, some companies choose to manage env variables / secrets differently. Some stores them in a HashiCorp instance, key-value stores like ETCD or Consul, or they even use a dedicated API to retrieve them during the startup.
Out of the box solution
Here we simply retrieve our env variables and we pass them to the Settings instance. We have to set extra='ignore' in the ModelConfig to prevent ValidationError if our external source stores extra env variables.
However, we lose the precedence feature of Pydantic BaseSettings: by injecting the env variables in the first position of the BaseSettings precedence chain, we can't override a variable using the environment.
Also, it's very complicated to handle multiple external sources as we have to merge them first.
Extending Pydantic BaseSettings
Pydantic already has a full precedence mechanism in the
_build_values
function:The solution would be to add the ability to add more arguments to the
deep_update
call, in the desired order (less relevant to most relevant source).These arguments would be
Callable[[BaseSettings], Dict[str, Optional[str]]]
. It's important to pass a reference to the BaseSettings instance in order to extract the list of relevant fields and the fields configuration (env_names, is_complex, ...).An example usage would be the following:
Here we load the env variables in the following order:
This allows us to re-use the precedence mechanism and opens the door to BaseSettings plugins. In my opinion, plugins would be a real benefit as Pydantic cannot support all secrets storage internally.
PR (#2107 ) implements this example.
I will be happy to answer comments, add missing documentation and discuss further about that feature proposal 🤗
The text was updated successfully, but these errors were encountered: