Skip to content
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

nested_model_default_partial_update documentation isn't clear #425

Closed
slingshotvfx opened this issue Sep 26, 2024 · 2 comments · Fixed by #433
Closed

nested_model_default_partial_update documentation isn't clear #425

slingshotvfx opened this issue Sep 26, 2024 · 2 comments · Fixed by #433
Assignees

Comments

@slingshotvfx
Copy link

The example given here doesn't actually change if you set nested_model_default_partial_update to False, so it's not clear to me what that flag actually does.

Playing around with it, it seems nested_model_default_partial_update only applies when a nested model has already been instantiated with non-default arguments. The documentation could be made clearer that when False, a new default instance of SubModel is created using only environment variables but when true, the existing SubModel instance is updated from the environment.

The code could be updated to, for example:

class SubModel(BaseModel):
    val: int = 0
    flag: bool = False


class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_nested_delimiter="__", nested_model_default_partial_update=True)

    nested_model: SubModel = SubModel(val=1)


class SettingsFalse(BaseSettings):
    model_config = SettingsConfigDict(env_nested_delimiter="__", nested_model_default_partial_update=False)

    nested_model: SubModel = SubModel(val=1)


# Apply a partial update to the default object using environment variables
os.environ["NESTED_MODEL__FLAG"] = "True"

print(Settings().model_dump())
print(SettingsFalse().model_dump())

output:

{'nested_model': {'val': 1, 'flag': True}}
{'nested_model': {'val': 0, 'flag': True}}
@hramezani
Copy link
Member

@kschwab Do you have time to check this issue?

@kschwab
Copy link
Contributor

kschwab commented Oct 1, 2024

Good catch @slingshotvfx, thanks for the issue. I've opened #433 to resolve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants