Skip to content

Commit

Permalink
Fix a regression in dotenv optional nested field (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani authored Sep 27, 2024
1 parent 76ba2c6 commit f3a25f2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,10 +986,19 @@ def __call__(self) -> dict[str, Any]:
for field_name, field in self.settings_cls.model_fields.items():
for _, field_env_name, _ in self._extract_field_info(field, field_name):
if env_name == field_env_name or (
lenient_issubclass(field.annotation, BaseModel) and env_name.startswith(field_env_name)
(
_annotation_is_complex(field.annotation, field.metadata)
or (
origin_is_union(get_origin(field.annotation))
and _union_is_complex(field.annotation, field.metadata)
)
)
and env_name.startswith(field_env_name)
):
env_used = True
break
if env_used:
break
if not env_used:
if is_extra_allowed and env_name.startswith(self.env_prefix):
# env_prefix should be respected and removed from the env_name
Expand Down
22 changes: 22 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5218,3 +5218,25 @@ class Settings(BaseSettings):

s = Settings()
assert s.model_dump() == {'nested': {'foo': ['one', 'two']}}


def test_dotenv_optional_nested(tmp_path):
p = tmp_path / '.env'
p.write_text('not_nested=works\nNESTED__A=fails\nNESTED__b=2')

class NestedSettings(BaseModel):
A: str
b: int

class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=p,
env_nested_delimiter='__',
extra='forbid',
)

not_nested: str
NESTED: Optional[NestedSettings]

s = Settings()
assert s.model_dump() == {'not_nested': 'works', 'NESTED': {'A': 'fails', 'b': 2}}

0 comments on commit f3a25f2

Please sign in to comment.