pydantic / pydantic-settings

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

Easily list the names of the env var required to set a field #292

Closed sotte closed 4 months ago

sotte commented 4 months ago

Is there an easy way to get/print the name of the env var that is used to set the value of a field?

Problem: You have a bunch of configs (potentially nested) and you quickly want to know what the name of the env var for a given field is.

Current workaround: Right now, I read the code, the nesting of the models, and the SettingsConfigDict to infer the name of the field. Then I try names until I find the correct one.

Potential solution: Add a new function like print_env_var_names() that works something like this:

class MyModel(

class MyModel(BaseSettings):
    model_config = SettingsConfigDict(env_prefix='FOO__')

    bar: int=5

print_env_var_names(MyModel)
# MyModel.bar -> FOO__BAR
hramezani commented 4 months ago

Thanks @sotte for this issue. It is not possible to list env var names right now. I think it would be a good addition and will help a lot. I think it can be a duplicate of https://github.com/pydantic/pydantic-settings/issues/287. I mean we can have a method on each sources and the method will generate required data structure for the source.

So, we can close this one as a duplicate.

What do you think @sotte ?

sotte commented 4 months ago

Thanks @hramezani. #287 is indeed very similar (though I would not have guessed that from just the headline). I'm closing this as duplicate.