Skip to content

BaseSettings: Customization of field value priority via Config #32

@espdev

Description

@espdev

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this feature/change is needed
  • After submitting this, I commit to one of:
    • Look through open issues and helped at least one other person
    • Hit the "watch" button on this repo to receive notifications and I commit to help at least 2 people that ask questions in the future
    • Implement a Pull Request for a confirmed bug

Feature Request

Currently, there is field value priority (from the docs):

In the case where a value is specified for the same Settings field in multiple ways, the selected value is determined as follows (in descending order of priority):

  1. Arguments passed to the Settings class initialiser.
  2. Environment variables, e.g. my_prefix_special_function as described above.
  3. Variables loaded from a dotenv (.env) file.
  4. The default field values for the Settings model.

However, in some cases it would be nice to swap 1 and (2,3) priorities. For example, I would like to create the configuration system based on config files (YAML, TOML, INI, etc) and env vars where envvars/dotenv have highest priority (env vars override values from config files that looks reasonable in some cases just like now env vars (2) override dotenv file (3)).

It might look something like this:

class Settings(BaseSettings):
    ...

s = Settings(**load_from_config_file(config_path))

In this case values from config file will override values from env vars.

Currently, the default behavior cannot be changed without subclassing BaseSettings class and overriding _build_values method. This is not mentioned in the current docs but previously it was mentioned (see pydantic/pydantic#340, pydantic/pydantic#341, pydantic/pydantic#343).

Would you mind considering the customization of this behavior via Config of BaseSettings model?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions