Skip to content

Commit

Permalink
Don't explode env vars if env_nested_delimiter is empty (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsakkis authored Feb 19, 2025
1 parent 7835118 commit 4b6fd3d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,9 @@ def explode_env_vars(self, field_name: str, field: FieldInfo, env_vars: Mapping[
Returns:
A dictionary contains extracted values from nested env values.
"""
if not self.env_nested_delimiter:
return {}

is_dict = lenient_issubclass(get_origin(field.annotation), dict)

prefixes = [
Expand Down
19 changes: 19 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,25 @@ class Settings(BaseSettings):
]


@pytest.mark.parametrize('env_nested_delimiter', [None, ''])
def test_case_sensitive_no_nested_delimiter(monkeypatch, env_nested_delimiter):
class Subsettings(BaseSettings):
foo: str

class Settings(BaseSettings):
subsettings: Subsettings

model_config = SettingsConfigDict(case_sensitive=True, env_nested_delimiter=env_nested_delimiter)

# Need to patch os.environ to get build to work on Windows, where os.environ is case insensitive
monkeypatch.setattr(os, 'environ', value={'subsettingsNonefoo': '1'})
with pytest.raises(ValidationError) as exc_info:
Settings()
assert exc_info.value.errors(include_url=False) == [
{'type': 'missing', 'loc': ('subsettings',), 'msg': 'Field required', 'input': {}}
]


def test_nested_dataclass(env):
@pydantic_dataclasses.dataclass
class DeepNestedDataclass:
Expand Down

0 comments on commit 4b6fd3d

Please sign in to comment.