Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a regresion in parsing env value for nested dict #301

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading