Skip to content

Environment Parsing with Default Nested Class #347

@kschwab

Description

@kschwab

@hramezani, is the below case expected or a bug? I would have thought them to be equivalent. I came across this when updating the CLI handling of defaults. I'll work to resolve it if it's a bug but wanted to confirm first before proceeding forward.

I added a potential fix if it is an issue.

import os from pydantic import BaseModel, ValidationError from pydantic_settings import BaseSettings class NestedA(BaseModel): v0: bool v1: bool class NestedB(BaseModel): v0: bool = False v1: bool = True class SettingsGood(BaseSettings, env_nested_delimiter='__'): nested: NestedB = NestedB() class SettingsBad(BaseSettings, env_nested_delimiter='__'): nested: NestedA = NestedA(v0=False, v1=True) os.environ['NESTED__V0'] = 'True' print(SettingsGood().model_dump()) """ {'nested': {'v0': True, 'v1': True}} """ try: print(SettingsBad().model_dump()) except ValidationError as e: print(e) """ 1 validation error for SettingsBad nested.v1  Field required [type=missing, input_value={'v0': 'True'}, input_type=dict] """

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions