Skip to content

Commit

Permalink
Fix bug in dotenv source when there is env with and without prefix (#440
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hramezani authored Oct 8, 2024
1 parent d2e498a commit a72fa73
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ def __call__(self) -> dict[str, Any]:
# As `extra` config is allowed in dotenv settings source, We have to
# update data with extra env variables from dotenv file.
for env_name, env_value in self.env_vars.items():
if not env_value:
if not env_value or env_name in data:
continue
env_used = False
for field_name, field in self.settings_cls.model_fields.items():
Expand Down
17 changes: 17 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2715,3 +2715,20 @@ class Settings(BaseSettings):

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


def test_dotenv_env_prefix_env_without_prefix(tmp_path):
p = tmp_path / '.env'
p.write_text('test_foo=test-foo\nfoo=foo')

class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=p,
env_prefix='TEST_',
extra='ignore',
)

foo: str

s = Settings()
assert s.model_dump() == {'foo': 'test-foo'}

0 comments on commit a72fa73

Please sign in to comment.