Skip to content

Commit 7ebb3bf

Browse files
authored
Separate decoding of complex values into specific method (#128)
1 parent 5acbdeb commit 7ebb3bf

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

pydantic_settings/sources.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,23 @@ def prepare_field_value(self, field_name: str, field: FieldInfo, value: Any, val
8484
The prepared value.
8585
"""
8686
if value is not None and (self.field_is_complex(field) or value_is_complex):
87-
return json.loads(value)
87+
return self.decode_complex_value(field_name, field, value)
8888
return value
8989

90+
def decode_complex_value(self, field_name: str, field: FieldInfo, value: Any) -> Any:
91+
"""
92+
Decode the value for a complex field
93+
94+
Args:
95+
field_name: The field name.
96+
field: The field.
97+
value: The value of the field that has to be prepared.
98+
99+
Returns:
100+
The decoded value for further preparation
101+
"""
102+
return json.loads(value)
103+
90104
@abstractmethod
91105
def __call__(self) -> dict[str, Any]:
92106
pass
@@ -414,7 +428,7 @@ def prepare_field_value(self, field_name: str, field: FieldInfo, value: Any, val
414428
else:
415429
# field is complex and there's a value, decode that as JSON, then add explode_env_vars
416430
try:
417-
value = json.loads(value)
431+
value = self.decode_complex_value(field_name, field, value)
418432
except ValueError as e:
419433
if not allow_parse_failure:
420434
raise e
@@ -516,7 +530,7 @@ def explode_env_vars(self, field_name: str, field: FieldInfo, env_vars: Mapping[
516530
is_complex, allow_json_failure = self._field_is_complex(target_field)
517531
if is_complex:
518532
try:
519-
env_val = json.loads(env_val)
533+
env_val = self.decode_complex_value(last_key, target_field, env_val)
520534
except ValueError as e:
521535
if not allow_json_failure:
522536
raise e

0 commit comments

Comments
 (0)