Skip to content

partial specify the sub model value does not work #363

@braindevices

Description

@braindevices
class SubModel(BaseModel):
    v1: str = "default"
    v2: bytes = b"hello"
    v3: int = 3

class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        cli_prog_name="prog1",
        cli_parse_args=True
    )

    v0: str = "ok"
    sub_model: SubModel = SubModel(v1="top default", v3=33)

sys.argv = [
    'example.py',
    "--sub_model.v1='cli'"
]
print(Settings())
# > v0='from env' sub_model=SubModel(v1="'cli'", v2=b'hello', v3=3)

expected value:

# > v0='from env' sub_model=SubModel(v1="'cli'", v2=b'hello', v3=33)

similar cause of the problem: if v3 does not have default in SubModel, it raise an error:

pydantic_core._pydantic_core.ValidationError: 1 validation error for Settings
sub_model.v3
  Field required [type=missing, input_value={'v1': "'cli'"}, input_type=dict]

I think we need to enable partial update with nested sub model, otherwise, the BaseSettings does not really support nested model field as cli arg or environment variable. If we decide not to support partial update, I guess it is better to drop the env_nested and --field.subfield idea completely. Because this behaviour, especially the silent fallback to SubModel's default will completely surprise the user.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions