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

fail to parse nested model when alias_generator is using #466

Closed
braindevices opened this issue Nov 4, 2024 · 4 comments · Fixed by #468
Closed

fail to parse nested model when alias_generator is using #466

braindevices opened this issue Nov 4, 2024 · 4 comments · Fixed by #468
Labels
bug Something isn't working

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,
        nested_model_default_partial_update=True,
        alias_generator=AliasGenerator(lambda s: s.replace('_', '-'))
    )

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

def help():
    sys.argv = [
        'example.py',
        "-h"
    ]
    try:
        Settings()
    except SystemExit:
        print("sys.exit() was called and handled.")
    print("After exit")

def test_cli():
    sys.argv = [
        'setting',
        "--sub-model.v1=cli",
        # "--sub1.v1=cli"
    ]
    
    print("in test cli: Settings()=", Settings())

def main():
    help()
    test_cli()

if __name__ == "__main__":
    main()

output:

usage: prog1 [-h] [--v0 str] [--sub-model JSON] [--sub-model.v1 str] [--sub-model.v2 bytes] [--sub-model.v3 int]

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: 33)

sys.exit() was called and handled.
After exit

Traceback (most recent call last):
  File ".//pydantic-setting-bug.py", line 47, in <module>
    main()
  File ".//pydantic-setting-bug.py", line 44, in main
    test_cli()
  File ".//pydantic-setting-bug.py", line 40, in test_cli
    print("in test cli: Settings()=", Settings())
  File "~/.local/lib/python3.10/site-packages/pydantic_settings/main.py", line 167, in __init__
    super().__init__(
  File "~/.local/lib/python3.10/site-packages/pydantic/main.py", line 212, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 2 validation errors for Settings
sub-model.v3
  Field required [type=missing, input_value={'v1': 'cli'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.9/v/missing
sub_model
  Extra inputs are not permitted [type=extra_forbidden, input_value={'v1': 'top default', 'v2': b'hello', 'v3': 33}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.9/v/extra_forbidden

pydantic-2.9.2 pydantic-core-2.23.4 pydantic_settings-2.6.1

@hramezani
Copy link
Member

@kschwab Could you please take a look here?

@kschwab
Copy link
Contributor

kschwab commented Nov 5, 2024

@hramezani was an issue with alias resolution in default settings source. Opened #468 to resolve.

@braindevices
Copy link
Author

@kschwab thanks a lot! it seems fixed:

pydnatic version: 2.9.2
pydantic_settings version: 2.6.1
usage: prog1 [-h] [--v0 str] [--sub-model JSON] [--sub-model.v1 str] [--sub-model.v2 bytes] [--sub-model.v3 int]

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: 33)
sys.exit() was called and handled.
After exit
['setting', '--sub-model.v1=cli']
in test cli: Settings()= v0='ok' sub_model=SubModel(v1='cli', v2=b'hello', v3=33)

@hramezani hramezani added bug Something isn't working and removed unconfirmed labels Nov 6, 2024
@hramezani
Copy link
Member

Thanks @kschwab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants