Skip to content
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