Skip to content

Commit 4247a14

Browse files
committed
Nit.
1 parent 03e2dd6 commit 4247a14

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

pydantic_settings/sources.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,16 @@ def _add_parser_args(
15631563
) -> ArgumentParser:
15641564
subparsers: Any = None
15651565
alias_path_args: dict[str, str] = {}
1566+
# Ignore model default if the default is a model and not a subclass of the current model.
1567+
model_default = (
1568+
None
1569+
if (
1570+
model_default is not PydanticUndefined
1571+
and (is_model_class(type(model_default)) or is_pydantic_dataclass(type(model_default)))
1572+
and not issubclass(type(model_default), model)
1573+
)
1574+
else model_default
1575+
)
15661576
for field_name, field_info in self._sort_arg_fields(model):
15671577
sub_models: list[type[BaseModel]] = self._get_sub_models(model, field_name, field_info)
15681578
alias_names, is_alias_path_only = _get_alias_names(
@@ -1615,17 +1625,6 @@ def _add_parser_args(
16151625
model_default=PydanticUndefined,
16161626
)
16171627
else:
1618-
# For union of models, ignore model default if not of same type as current model.
1619-
model_default = (
1620-
None
1621-
if (
1622-
model_default is not PydanticUndefined
1623-
and (is_model_class(type(model_default)) or is_pydantic_dataclass(type(model_default)))
1624-
and not issubclass(type(model_default), model)
1625-
)
1626-
else model_default
1627-
)
1628-
16291628
flag_prefix: str = self._cli_flag_prefix
16301629
is_append_action = _annotation_contains_types(
16311630
field_info.annotation, (list, set, dict, Sequence, Mapping), is_strip_annotated=True

0 commit comments

Comments
 (0)