Skip to content

Commit

Permalink
Fix a regresion in parsing env value for nested dict (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Jun 5, 2024
1 parent bcbfd17 commit ad07a57
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ def explode_env_vars(self, field_name: str, field: FieldInfo, env_vars: Mapping[
is_complex, allow_json_failure = self._field_is_complex(target_field)
else:
# nested field type is dict
is_complex, allow_json_failure = True, False
is_complex, allow_json_failure = True, True
if is_complex:
try:
env_val = self.decode_complex_value(last_key, target_field, env_val) # type: ignore
Expand Down
11 changes: 11 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3789,6 +3789,17 @@ class Settings(BaseSettings):
assert s.model_dump() == {'nested': {'foo': {'a': 1}}, 'sub_dict': {'bar': {'foo': {'b': 2}}}}


def test_env_nested_dict_value(env):
class Settings(BaseSettings):
nested: Dict[str, Dict[str, Dict[str, str]]]

model_config = SettingsConfigDict(env_nested_delimiter='__')

env.set('nested__foo__a__b', 'bar')
s = Settings()
assert s.model_dump() == {'nested': {'foo': {'a': {'b': 'bar'}}}}


def test_nested_models_leaf_vs_deeper_env_dict_assumed(env):
class NestedSettings(BaseModel):
foo: str
Expand Down

0 comments on commit ad07a57

Please sign in to comment.