Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in dotenv source when there is env with and without prefix #440

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'}
Loading