Skip to content

Commit

Permalink
Cli prefix validation alias fix (pydantic#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
kschwab authored Sep 2, 2024
1 parent 47924f5 commit 818d56e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
23 changes: 13 additions & 10 deletions pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,16 +1604,19 @@ def _add_parser_args(
resolved_names,
model_default=model_default,
)
elif is_alias_path_only:
continue
elif group is not None:
if isinstance(group, dict):
group = self._add_argument_group(parser, **group)
added_args += list(arg_names)
self._add_argument(group, *(f'{arg_flag[:len(name)]}{name}' for name in arg_names), **kwargs)
else:
added_args += list(arg_names)
self._add_argument(parser, *(f'{arg_flag[:len(name)]}{name}' for name in arg_names), **kwargs)
elif not is_alias_path_only:
if arg_prefix and field_info.validation_alias is not None:
# Strip prefix if validation alias is set and value is not complex.
# Related https://github.com/pydantic/pydantic-settings/pull/25
kwargs['dest'] = kwargs['dest'][self.env_prefix_len :]
if group is not None:
if isinstance(group, dict):
group = self._add_argument_group(parser, **group)
added_args += list(arg_names)
self._add_argument(group, *(f'{arg_flag[:len(name)]}{name}' for name in arg_names), **kwargs)
else:
added_args += list(arg_names)
self._add_argument(parser, *(f'{arg_flag[:len(name)]}{name}' for name in arg_names), **kwargs)

self._add_parser_alias_paths(parser, alias_path_args, added_args, arg_prefix, subcommand_prefix, group)
return parser
Expand Down
12 changes: 12 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,18 @@ class Settings(BaseSettings):
assert Settings().foobar == 'bar'


def test_validation_alias_with_cli_prefix():
class Settings(BaseSettings, cli_exit_on_error=False):
foobar: str = Field(validation_alias='foo')

model_config = SettingsConfigDict(cli_prefix='p')

with pytest.raises(SettingsError, match='error parsing CLI: unrecognized arguments: --foo bar'):
Settings(_cli_parse_args=['--foo', 'bar'])

assert Settings(_cli_parse_args=['--p.foo', 'bar']).foobar == 'bar'


def test_case_sensitive(monkeypatch):
class Settings(BaseSettings):
foo: str
Expand Down

0 comments on commit 818d56e

Please sign in to comment.