pydantic / pydantic-settings

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

Comma delimited string as list of strings #291

Closed romanzdk closed 4 months ago

romanzdk commented 4 months ago

I use to define settings class like e.g.

class CoolSettings(pydantic_settings.BaseSettings):
    COOL_LIST: list[str] = ['xy', 'ab']

Would it be possible to pass COOL_LIST env var as comma separated string? I.e. I would define my env var as COOL_LIST=abc,def and it would be parsed to list of strings? Of course I would have to specify the separator? I recon COOL_LIST='["abc","def"]' works but is not very user friendly to write. Or ofc custom parser could be written.

hramezani commented 4 months ago

Thanks @romanzdk for this issue. Right now it is not possible to load a list from comma-separated env value. you can create a custom source class, inherit from EnvSettingsSource and override the prepare_field_value