pydantic / pydantic-settings

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

Question about the parameter inference for the constructor in the class inherited from BaseSettings #334

Closed laipz8200 closed 1 week ago

laipz8200 commented 2 weeks ago

Hi!

There are many parameters in BaseSettings such as _env_file and _case_sensitive, but these parameters are not fields of BaseSettings. According to the definition from the Python documentation, the __init__ method of my own Settings class will not include these parameters unless I write them myself.

As a result, when I inherit from BaseSettings and create my own Settings class, once I pass parameters like _env_file to the Settings, the type checker(both Pyright and Mypy) will complain.

image

Do you have any plans to fix it? Or should we include these parameters as fields in the BaseSettings class? (I have tried this approach, it solves the problem but doesn't look very elegant.)

hramezani commented 1 week ago

Hi,

There are many parameters in BaseSettings such as _env_file and _case_sensitive, but these parameters are not fields of BaseSettings. According to the definition from the Python documentation, the init method of my own Settings class will not include these parameters unless I write them myself.

These are configuration params that you can pass when initializing your setting class. we have same params in SettingsConfigDict as well but these are the params that have more priority over SettingsConfigDict params.

As a result, when I inherit from BaseSettings and create my own Settings class, once I pass parameters like _env_file to the Settings, the type checker(both Pyright and Mypy) will complain.

Yes, you will get the error with pyright but I think you don't get error in mypy if you enable pydantic mypy plugin

laipz8200 commented 1 week ago

@hramezani Thank you for your reply!