Skip to content

Commit 66ecc3a

Browse files
authored
Move preferred alias resolution to private method (#507)
1 parent 95fae54 commit 66ecc3a

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

pydantic_settings/sources.py

+32-17
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def get_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str,
279279
field_name: The field name.
280280
281281
Returns:
282-
A tuple contains the key, value and a flag to determine whether value is complex.
282+
A tuple that contains the value, key and a flag to determine whether value is complex.
283283
"""
284284
pass
285285

@@ -562,12 +562,35 @@ def _replace_env_none_type_values(self, field_value: dict[str, Any]) -> dict[str
562562

563563
return values
564564

565+
def _get_resolved_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str, bool]:
566+
"""
567+
Gets the value, the preferred alias key for model creation, and a flag to determine whether value
568+
is complex.
569+
570+
Note:
571+
In V3, this method should either be made public, or, this method should be removed and the
572+
abstract method get_field_value should be updated to include a "use_preferred_alias" flag.
573+
574+
Args:
575+
field: The field.
576+
field_name: The field name.
577+
578+
Returns:
579+
A tuple that contains the value, preferred key and a flag to determine whether value is complex.
580+
"""
581+
field_value, field_key, value_is_complex = self.get_field_value(field, field_name)
582+
if not (value_is_complex or (self.config.get('populate_by_name', False) and (field_key == field_name))):
583+
field_infos = self._extract_field_info(field, field_name)
584+
preferred_key, *_ = field_infos[0]
585+
return field_value, preferred_key, value_is_complex
586+
return field_value, field_key, value_is_complex
587+
565588
def __call__(self) -> dict[str, Any]:
566589
data: dict[str, Any] = {}
567590

568591
for field_name, field in self.settings_cls.model_fields.items():
569592
try:
570-
field_value, field_key, value_is_complex = self.get_field_value(field, field_name)
593+
field_value, field_key, value_is_complex = self._get_resolved_field_value(field, field_name)
571594
except Exception as e:
572595
raise SettingsError(
573596
f'error getting value for field "{field_name}" from source "{self.__class__.__name__}"'
@@ -675,13 +698,11 @@ def get_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str,
675698
field_name: The field name.
676699
677700
Returns:
678-
A tuple contains the key, value if the file exists otherwise `None`, and
701+
A tuple that contains the value (`None` if the file does not exist), key, and
679702
a flag to determine whether value is complex.
680703
"""
681704

682-
field_infos = self._extract_field_info(field, field_name)
683-
preferred_key, *_ = field_infos[0]
684-
for field_key, env_name, value_is_complex in field_infos:
705+
for field_key, env_name, value_is_complex in self._extract_field_info(field, field_name):
685706
# paths reversed to match the last-wins behaviour of `env_file`
686707
for secrets_path in reversed(self.secrets_paths):
687708
path = self.find_case_path(secrets_path, env_name, self.case_sensitive)
@@ -690,16 +711,14 @@ def get_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str,
690711
continue
691712

692713
if path.is_file():
693-
if value_is_complex or (self.config.get('populate_by_name', False) and (field_key == field_name)):
694-
preferred_key = field_key
695-
return path.read_text().strip(), preferred_key, value_is_complex
714+
return path.read_text().strip(), field_key, value_is_complex
696715
else:
697716
warnings.warn(
698717
f'attempted to load secret file "{path}" but found a {path_type_label(path)} instead.',
699718
stacklevel=4,
700719
)
701720

702-
return None, preferred_key, value_is_complex
721+
return None, field_key, value_is_complex
703722

704723
def __repr__(self) -> str:
705724
return f'{self.__class__.__name__}(secrets_dir={self.secrets_dir!r})'
@@ -742,21 +761,17 @@ def get_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str,
742761
field_name: The field name.
743762
744763
Returns:
745-
A tuple contains the key, value if the file exists otherwise `None`, and
764+
A tuple that contains the value (`None` if not found), key, and
746765
a flag to determine whether value is complex.
747766
"""
748767

749768
env_val: str | None = None
750-
field_infos = self._extract_field_info(field, field_name)
751-
preferred_key, *_ = field_infos[0]
752-
for field_key, env_name, value_is_complex in field_infos:
769+
for field_key, env_name, value_is_complex in self._extract_field_info(field, field_name):
753770
env_val = self.env_vars.get(env_name)
754771
if env_val is not None:
755-
if value_is_complex or (self.config.get('populate_by_name', False) and (field_key == field_name)):
756-
preferred_key = field_key
757772
break
758773

759-
return env_val, preferred_key, value_is_complex
774+
return env_val, field_key, value_is_complex
760775

761776
def prepare_field_value(self, field_name: str, field: FieldInfo, value: Any, value_is_complex: bool) -> Any:
762777
"""

0 commit comments

Comments
 (0)