Skip to content

Commit d098efd

Browse files
authored
Merge branch 'main' into main
2 parents 9d1e5d3 + 63cd453 commit d098efd

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

pydantic_settings/sources/providers/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def get_enum_names(
144144
for type_ in get_args(annotation):
145145
enum_names += cls.get_enum_names(type_, kebab_case)
146146
if annotation and _lenient_issubclass(annotation, Enum):
147-
enum_names += tuple(cls.get_kebab_case(val.name, kebab_case == 'all') for val in annotation)
147+
enum_names += tuple(cls.get_kebab_case(name, kebab_case == 'all') for name in annotation.__members__.keys())
148148
return enum_names
149149

150150
def subcommand_alias(self, sub_model: type[BaseModel]) -> str:
@@ -1255,7 +1255,7 @@ def _metavar_format_recurse(self, obj: Any) -> str:
12551255
return self._metavar_format_choices(list(map(str, self._get_modified_args(obj))))
12561256
elif _lenient_issubclass(obj, Enum):
12571257
return self._metavar_format_choices(
1258-
[_CliArg.get_kebab_case(val.name, self.cli_kebab_case == 'all') for val in obj]
1258+
[_CliArg.get_kebab_case(name, self.cli_kebab_case == 'all') for name in obj.__members__.keys()]
12591259
)
12601260
elif isinstance(obj, _WithArgsTypes):
12611261
return self._metavar_format_choices(

pydantic_settings/sources/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ def _strip_annotated(annotation: Any) -> Any:
135135
def _annotation_enum_val_to_name(annotation: type[Any] | None, value: Any) -> str | None:
136136
for type_ in (annotation, get_origin(annotation), *get_args(annotation)):
137137
if _lenient_issubclass(type_, Enum):
138-
if value in tuple(val.value for val in type_):
138+
if value in type_.__members__.values():
139139
return type_(value).name
140140
return None
141141

142142

143143
def _annotation_enum_name_to_val(annotation: type[Any] | None, name: Any) -> Any:
144144
for type_ in (annotation, get_origin(annotation), *get_args(annotation)):
145145
if _lenient_issubclass(type_, Enum):
146-
if name in tuple(val.name for val in type_):
146+
if name in type_.__members__.keys():
147147
return type_[name]
148148
return None
149149

tests/test_source_cli.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,7 @@ class Pet(IntEnum):
14441444
dog = 0
14451445
cat = 1
14461446
bird = 2
1447+
crow = 2
14471448

14481449
class Cfg(BaseSettings):
14491450
pet: Pet = Pet.dog
@@ -1452,15 +1453,18 @@ class Cfg(BaseSettings):
14521453
cfg = CliApp.run(Cfg, cli_args=['--pet', 'cat', '--union_pet', 'dog'])
14531454
assert cfg.model_dump() == {'pet': Pet.cat, 'union_pet': Pet.dog}
14541455

1456+
cfg = CliApp.run(Cfg, cli_args=['--pet', 'crow', '--union_pet', 'dog'])
1457+
assert cfg.model_dump() == {'pet': Pet.crow, 'union_pet': Pet.dog}
1458+
14551459
with pytest.raises(ValidationError) as exc_info:
14561460
CliApp.run(Cfg, cli_args=['--pet', 'rock'])
14571461
assert exc_info.value.errors(include_url=False) == [
14581462
{
14591463
'type': 'enum',
14601464
'loc': ('pet',),
1461-
'msg': 'Input should be 0, 1 or 2',
1465+
'msg': 'Input should be 0, 1, 2 or 2',
14621466
'input': 'rock',
1463-
'ctx': {'expected': '0, 1 or 2'},
1467+
'ctx': {'expected': '0, 1, 2 or 2'},
14641468
}
14651469
]
14661470

@@ -1469,22 +1473,23 @@ class Cfg(BaseSettings):
14691473

14701474
if PYTHON_3_14:
14711475
if IS_WINDOWS:
1472-
text = ' [--union_pet {{dog,cat,bird},int}]'
1476+
text = ' [--union_pet {{dog,cat,bird,crow},int}]'
14731477
else:
1474-
text = ' [--union_pet {{dog,cat,bird},int}]'
1478+
text = ' [--union_pet {{dog,cat,bird,crow},int}]'
14751479
else:
1476-
text = ' [--union_pet {{dog,cat,bird},int}]'
1480+
text = ' [--union_pet {{dog,cat,bird,crow},int}]'
14771481
with pytest.raises(SystemExit):
14781482
CliApp.run(Cfg)
14791483
assert (
14801484
sanitize_cli_output(capsys.readouterr().out)
1481-
== f"""usage: example.py [-h] [--pet {{dog,cat,bird}}]
1485+
== f"""usage: example.py [-h] [--pet {{dog,cat,bird,crow}}]
14821486
{text}
14831487
14841488
{ARGPARSE_OPTIONS_TEXT}:
14851489
-h, --help show this help message and exit
1486-
--pet {{dog,cat,bird}} (default: dog)
1487-
--union_pet {{{{dog,cat,bird}},int}}
1490+
--pet {{dog,cat,bird,crow}}
1491+
(default: dog)
1492+
--union_pet {{{{dog,cat,bird,crow}},int}}
14881493
(default: 43)
14891494
"""
14901495
)

0 commit comments

Comments
 (0)