Skip to content

help message of sub model in basesetting do not show the proper default value if the top field has a default #362

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

Closed
braindevices opened this issue Aug 8, 2024 · 2 comments · Fixed by #364
Assignees

Comments

@braindevices
Copy link

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

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

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

sys.argv = [
        'example.py',
        "-h"
    ]
Settings()

It show help:

...
  --sub_model.v1 str    (default: default)
  --sub_model.v2 bytes  (default: b'hello')
  --sub_model.v3 int    (required)

This is apparently wrong, because the default values:

sys.argv = [
        'example.py',
    ]
print(Settings())
#> v0='ok' sub_model=SubModel(v1='top default', v2=b'hello', v3=3)

The help message should use the actual top level default values instead of the sub model's own default values.

We expect:

...
  --sub_model.v1 str    (default: top default)
  --sub_model.v2 bytes  (default: b'hello')
  --sub_model.v3 int    (default: 3)
@braindevices braindevices changed the title help message of sub model in basesetting do not should the proper default value if the field of the sub model does not have default help message of sub model in basesetting do not show the proper default value if the field of the sub model does not have default Aug 8, 2024
@braindevices braindevices changed the title help message of sub model in basesetting do not show the proper default value if the field of the sub model does not have default help message of sub model in basesetting do not show the proper default value if the top field has a default Aug 8, 2024
@hramezani
Copy link
Member

Thanks @braindevices for reporting this issue.

I think the help extracts default values from the model definition.
@kschwab would it be too complex to consider defaults in sub field definition?

@kschwab
Copy link
Contributor

kschwab commented Aug 9, 2024

@hramezani, no, I've opened the PR. This is the fix I was discussing in #347, where the CLI will now properly display the defaults:

options:
  -h, --help            show this help message and exit
  --v0 str              (default: ok)

sub_model options:
  --sub_model JSON      set sub_model from JSON string
  --sub_model.v1 str    (default: top default)
  --sub_model.v2 bytes  (default: b'hello')
  --sub_model.v3 int    (default: 3)

However, without fixing #347, it will not function properly:

sys.argv = [
        'example.py',
        '--sub_model.v1=oops',
    ]
Settings()
"""
pydantic_core._pydantic_core.ValidationError: 1 validation error for Settings
sub_model.v3
  Field required [type=missing, input_value={'v1': 'oops'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.7/v/missing
"""

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.

3 participants