Skip to content

Commit fc8a694

Browse files
authored
fix: Deterministic alias selection when using validate_by_name (#707)
1 parent 584983d commit fc8a694

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

pydantic_settings/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,9 @@ def _settings_restore_init_kwarg_names(
466466
for field_name, field_info in settings_cls.model_fields.items():
467467
alias_names, *_ = _get_alias_names(field_name, field_info)
468468
matchable_names = set(alias_names)
469-
include_name = settings_cls.model_config.get('populate_by_name', False)
469+
include_name = settings_cls.model_config.get(
470+
'populate_by_name', False
471+
) or settings_cls.model_config.get('validate_by_name', False)
470472
if include_name:
471473
matchable_names.add(field_name)
472474
init_kwarg_name = init_kwarg_names & matchable_names

pydantic_settings/sources/base.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ def __init__(
267267
# When populate_by_name is True, allow using the field name as an input key,
268268
# but normalize to the preferred alias to keep keys consistent across sources.
269269
matchable_names = set(alias_names)
270-
include_name = settings_cls.model_config.get('populate_by_name', False)
270+
include_name = settings_cls.model_config.get('populate_by_name', False) or settings_cls.model_config.get(
271+
'validate_by_name', False
272+
)
271273
if include_name:
272274
matchable_names.add(field_name)
273275
init_kwarg_name = init_kwarg_names & matchable_names
@@ -369,7 +371,7 @@ def _extract_field_info(self, field: FieldInfo, field_name: str) -> list[tuple[s
369371
else: # string validation alias
370372
field_info.append((v_alias, self._apply_case_sensitive(v_alias), False))
371373

372-
if not v_alias or self.config.get('populate_by_name', False):
374+
if not v_alias or self.config.get('populate_by_name', False) or self.config.get('validate_by_name', False):
373375
annotation = field.annotation
374376
if typing_objects.is_typealiastype(annotation) or typing_objects.is_typealiastype(get_origin(annotation)):
375377
annotation = _strip_annotated(annotation.__value__) # type: ignore[union-attr]
@@ -489,7 +491,13 @@ def _get_resolved_field_value(self, field: FieldInfo, field_name: str) -> tuple[
489491
A tuple that contains the value, preferred key and a flag to determine whether value is complex.
490492
"""
491493
field_value, field_key, value_is_complex = self.get_field_value(field, field_name)
492-
if not (value_is_complex or (self.config.get('populate_by_name', False) and (field_key == field_name))):
494+
if not (
495+
value_is_complex
496+
or (
497+
(self.config.get('populate_by_name', False) or self.config.get('validate_by_name', False))
498+
and (field_key == field_name)
499+
)
500+
):
493501
field_infos = self._extract_field_info(field, field_name)
494502
preferred_key, *_ = field_infos[0]
495503
return field_value, preferred_key, value_is_complex

0 commit comments

Comments
 (0)