Skip to content

Commit

Permalink
fix: superfluous deep env conflicts with non-dict model leaf (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
diefans authored Apr 23, 2024
1 parent a4aadfd commit 2d2f94f
Show file tree
Hide file tree
Showing 2 changed files with 20 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 @@ -638,7 +638,8 @@ def explode_env_vars(self, field_name: str, field: FieldInfo, env_vars: Mapping[
except ValueError as e:
if not allow_json_failure:
raise e
env_var[last_key] = env_val
if isinstance(env_var, dict):
env_var[last_key] = env_val

return result

Expand Down
18 changes: 18 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2586,3 +2586,21 @@ class Settings(BaseSettings):
env.set('sub_dict__bar__foo', '{"b": 2}')
s = Settings()
assert s.model_dump() == {'nested': {'foo': {'a': 1}}, 'sub_dict': {'bar': {'foo': {'b': 2}}}}


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

class Settings(BaseSettings):
model_config = SettingsConfigDict(env_nested_delimiter='__')

nested: NestedSettings

env.set('nested__foo', 'string')
env.set(
'nested__foo__bar',
'this should not be evaluated, since foo is a string by annotation and not a dict',
)
s = Settings()
assert s.model_dump() == {'nested': {'foo': 'string'}}

0 comments on commit 2d2f94f

Please sign in to comment.