Skip to content

Commit

Permalink
fix: a second level of environment nesting expected a dict (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
diefans committed Apr 24, 2024
1 parent 2d2f94f commit 1ffbd95
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,8 @@ def explode_env_vars(self, field_name: str, field: FieldInfo, env_vars: Mapping[
target_field: FieldInfo | None = field
for key in keys:
target_field = self.next_field(target_field, key)
env_var = env_var.setdefault(key, {})
if isinstance(env_var, dict):
env_var = env_var.setdefault(key, {})

# get proper field with last_key
target_field = self.next_field(target_field, last_key)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2602,5 +2602,9 @@ class Settings(BaseSettings):
'nested__foo__bar',
'this should not be evaluated, since foo is a string by annotation and not a dict',
)
env.set(
'nested__foo__bar__baz',
'one more',
)
s = Settings()
assert s.model_dump() == {'nested': {'foo': 'string'}}

0 comments on commit 1ffbd95

Please sign in to comment.