@@ -279,7 +279,7 @@ def get_field_value(self, field: FieldInfo, field_name: str) -> tuple[Any, str,
279
279
field_name: The field name.
280
280
281
281
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.
283
283
"""
284
284
pass
285
285
@@ -562,12 +562,35 @@ def _replace_env_none_type_values(self, field_value: dict[str, Any]) -> dict[str
562
562
563
563
return values
564
564
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
+
565
588
def __call__ (self ) -> dict [str , Any ]:
566
589
data : dict [str , Any ] = {}
567
590
568
591
for field_name , field in self .settings_cls .model_fields .items ():
569
592
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 )
571
594
except Exception as e :
572
595
raise SettingsError (
573
596
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,
675
698
field_name: The field name.
676
699
677
700
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
679
702
a flag to determine whether value is complex.
680
703
"""
681
704
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 ):
685
706
# paths reversed to match the last-wins behaviour of `env_file`
686
707
for secrets_path in reversed (self .secrets_paths ):
687
708
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,
690
711
continue
691
712
692
713
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
696
715
else :
697
716
warnings .warn (
698
717
f'attempted to load secret file "{ path } " but found a { path_type_label (path )} instead.' ,
699
718
stacklevel = 4 ,
700
719
)
701
720
702
- return None , preferred_key , value_is_complex
721
+ return None , field_key , value_is_complex
703
722
704
723
def __repr__ (self ) -> str :
705
724
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,
742
761
field_name: The field name.
743
762
744
763
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
746
765
a flag to determine whether value is complex.
747
766
"""
748
767
749
768
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 ):
753
770
env_val = self .env_vars .get (env_name )
754
771
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
757
772
break
758
773
759
- return env_val , preferred_key , value_is_complex
774
+ return env_val , field_key , value_is_complex
760
775
761
776
def prepare_field_value (self , field_name : str , field : FieldInfo , value : Any , value_is_complex : bool ) -> Any :
762
777
"""
0 commit comments