pydantic / pydantic-settings

Settings management using pydantic
https://docs.pydantic.dev/latest/usage/pydantic_settings/
MIT License
634 stars 66 forks source link

Support alternative value resolution for BaseSettings #1

Closed ofek closed 1 year ago

ofek commented 2 years ago

https://github.com/pydantic/pydantic/issues/1676


Feature Request

My use case is I'm reading a toml config file and passing what is read directly to the model constructor. However, I have no way to allow users env var or .env overrides.

Essentially, configs persisted to disk cannot benefit from BaseSettings.

Viicos commented 1 year ago

Hi @ofek, the way I'm currently doing it can be found here (basically defining a custom source, and rearranging the order in customise_sources). The only drawback is that I can't specify the filename (it is read from an envvar), but this might be useful still for your use case.

alextremblay commented 1 year ago

I've got a working implementation here that might be of interest:

https://github.com/uoft-networking/tools/blob/b1c67d6ce8def1b67af5b3ca08fe7357f595bb68/projects/core/uoft_core/__init__.py#L1209

The codebase is a mess, but it works.

Among other things it:

If there's interest, I'd love to be able to clean it up and merge into pydantic-settings.

Right now I'm in the process of updating it to pydantic V2

hramezani commented 1 year ago

It is possible to have a custom settings source class beside the internal pydantic-settings source classes.

You can find the related docs here : https://docs.pydantic.dev/latest/concepts/pydantic_settings/#customise-settings-sources

ofek commented 1 year ago

Awesome, thank you!