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
7 changes: 6 additions & 1 deletion pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,17 @@ def __call__(self) -> dict[str, Any]:
data: dict[str, Any] = super().__call__()

data_lower_keys: list[str] = []
is_extra_allowed = self.config.get('extra') != 'forbid'
if not self.case_sensitive:
data_lower_keys = [x.lower() for x in data.keys()]

# As `extra` config is allowed in dotenv settings source, We have to
# update data with extra env variabels from dotenv file.
for env_name, env_value in self.env_vars.items():
if not is_extra_allowed and not env_name.startswith(self.env_prefix):
raise SettingsError(
"unable to load environment variables from dotenv file "
f"due to the presence of variables without the specified prefix - '{self.env_prefix}'"
)
if env_name.startswith(self.env_prefix) and env_value is not None:
env_name_without_prefix = env_name[self.env_prefix_len :]
first_key, *_ = env_name_without_prefix.split(self.env_nested_delimiter)
Expand Down
49 changes: 48 additions & 1 deletion tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,6 @@ class Settings(BaseSettings):

prefix_b='better string'
prefix_c="best string"
f="random value"
"""


Expand All @@ -790,6 +789,54 @@ class Settings(BaseSettings):
assert s.c == 'best string'


prefix_test_env_invalid_file = """\
# this is a comment
prefix_A=good string
# another one, followed by whitespace

prefix_b='better string'
prefix_c="best string"
f="random value"
"""


def test_env_file_with_env_prefix_invalid(tmp_path):
p = tmp_path / '.env'
p.write_text(prefix_test_env_invalid_file)

class Settings(BaseSettings):
a: str
b: str
c: str

model_config = SettingsConfigDict(env_file=p, env_prefix='prefix_')

err_msg = (
"unable to load environment variables from dotenv file "
"due to the presence of variables without the specified prefix - 'prefix_'"
)
with pytest.raises(SettingsError, match=err_msg):
Settings()


def test_ignore_env_file_with_env_prefix_invalid(tmp_path):
p = tmp_path / '.env'
p.write_text(prefix_test_env_invalid_file)

class Settings(BaseSettings):
a: str
b: str
c: str

model_config = SettingsConfigDict(env_file=p, env_prefix='prefix_', extra='ignore')

s = Settings()

assert s.a == 'good string'
assert s.b == 'better string'
assert s.c == 'best string'


def test_env_file_config_case_sensitive(tmp_path):
p = tmp_path / '.env'
p.write_text(test_env_file)
Expand Down