Closed SantiagoG closed 1 month ago
Thanks @SantiagoG for reporting this.
This is probably related to pydantic-settings
new CLI source.
@kschwab Could you please take a look at this? this shouldn't happen because the code does not enable the CLI source and we mentioned in the doc
To enable CLI parsing, we simply set the `cli_parse_args` flag to a valid value, which retains similar conotations as
defined in `argparse`.
Probably we need to check cli_parse_args
soon and return fast in CliSettingsSource.__init__
or even don't initialize it
@hramezani yes agreed. It's currently getting initialized; we can move it to only initialize if parsing is necessary.
The above is also a bug in CliSettingsSource
related to AliasChoices
which we can address separately.
the fix has been released in new pydantic-settings 2.3.2
Description
Field aliases fail when subclassing a model with BaseSettings and using AliasChoices.
Pytest output
```shell /usr/local/lib/python3.11/dist-packages/pydantic_settings/main.py:141: in __init__ **__pydantic_self__._settings_build_values( /usr/local/lib/python3.11/dist-packages/pydantic_settings/main.py:260: in _settings_build_values CliSettingsSource( /usr/local/lib/python3.11/dist-packages/pydantic_settings/sources.py:902: in __init__ self._connect_root_parser( /usr/local/lib/python3.11/dist-packages/pydantic_settings/sources.py:1236: in _connect_root_parser self._add_parser_args( /usr/local/lib/python3.11/dist-packages/pydantic_settings/sources.py:1255: in _add_parser_args for field_name, resolved_name, field_info in self._sort_arg_fields(model): /usr/local/lib/python3.11/dist-packages/pydantic_settings/sources.py:1154: in _sort_arg_fields resolved_name = resolved_name.lower() if not self.case_sensitive else resolved_name E AttributeError: 'AliasChoices' object has no attribute 'lower'\ self = EnvSettingsSource(env_nested_delimiter='.', env_prefix_len=0), model =Example Code
Expect
For this test to pass with BaseSettings as it does so with BaseModel.