-
-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BaseSettings: Environment variable is not loaded in nested model if defined in Field()
#27
Comments
Field()
Field()
Thanks for reporting. Looks like if this is simple to fix in V1.10, happy to accept a PR. We should also make sure this is fixed in V2 with the new pydantic-settings package. |
Does this mean I would have to create a branch from Sorry, I haven't contributed yet to this great project 🙃 |
Yes, you need to create a PR based on |
I think it shouldn't work like you expect because you are initializing FYI, pydantic-settings now is a separate package and is in alpha state. you can install it by Here is your example in pydantic-settings: from pydantic import BaseModel, ConfigDict, Field
from pydantic_settings import BaseSettings
class SubModel(BaseModel):
sub_var1: str = Field(validation_alias="SUB_VAR")
sub_var2: str = "bar"
class Settings(BaseSettings):
main_var: str = Field(validation_alias="MAIN_VAR")
nested: SubModel
model_config = ConfigDict(env_file='.env', env_nested_delimiter='__')
print(Settings().model_dump()) |
Initial Checks
Description
I have a class
BaseSettings
with a nestedBaseModel
. Within the nested model (namednested
), I defined a variable withsub_var1: str = Field(..., env="SUB_VAR")
(as mentioned in the docs).I would expect that the environment variable name specified in the nested
Field
is loaded from my.env
file. But it's not (see the example below). My.env
file looks as follows:It only works with
env_nested_delimiter='__'
and an environment variableNESTED__SUB_VAR1
but I would expect it to work withSUB_VAR
(as it's set in theField
).I'm not sure if this is a bug or expected behavior. If it's the latter I'm doing a feature request, I guess.
Example Code
Python, Pydantic & OS Version
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: