You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Field aliases fail when subclassing a model with BaseSettings and using AliasChoices.
Pytest output
/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
forfield_name, resolved_name, field_infoin 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 = <class 'test_pydantic-settings.test_settings.<locals>.Check'>
def _sort_arg_fields(self, model: type[BaseModel]) -> list[tuple[str, str, FieldInfo]]:
positional_args, subcommand_args, optional_args = [], [], []
fields = model.__pydantic_fields__ if is_pydantic_dataclass(model) else model.model_fields
forfield_name, field_infoinfields.items():
resolved_name = field_name if field_info.alias is None else field_info.alias
> resolved_name = resolved_name.lower() if not self.case_sensitive else resolved_name
E AttributeError: 'AliasChoices' object has no attribute 'lower'
Example Code
importpytestfrompydanticimportAliasChoices, Field# from pydantic import BaseModelfrompydantic_settingsimportBaseSettingsdeftest_settings():
classCheck(BaseSettings):
# class Check(BaseModel): # This worksfield: str=Field(
"foo",
alias=AliasChoices(
"ALIAS_NAME"
)
)
assertCheck().field=='foo'assertCheck(ALIAS_NAME="bar").field=='bar'
Expect
For this test to pass with BaseSettings as it does so with BaseModel.
Description
Field aliases fail when subclassing a model with BaseSettings and using AliasChoices.
Pytest output
Example Code
Expect
For this test to pass with BaseSettings as it does so with BaseModel.
The text was updated successfully, but these errors were encountered: