Skip to content

Commit

Permalink
respect env_prefix when extra allowed (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzstoatzz authored Feb 19, 2024
1 parent 965d1b4 commit 859805a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pydantic_settings/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,10 @@ def _read_env_files(self) -> Mapping[str, str | None]:

def __call__(self) -> dict[str, Any]:
data: dict[str, Any] = super().__call__()
is_extra_allowed = self.config.get('extra') != 'forbid'

# As `extra` config is allowed in dotenv settings source, We have to
# update data with extra env variabels from dotenv file.
# update data with extra env variables from dotenv file.
for env_name, env_value in self.env_vars.items():
if not env_value:
continue
Expand All @@ -696,7 +697,12 @@ def __call__(self) -> dict[str, Any]:
env_used = True
break
if not env_used:
data[env_name] = env_value
if is_extra_allowed and env_name.startswith(self.env_prefix):
# env_prefix should be respected and removed from the env_name
normalized_env_name = env_name[len(self.env_prefix) :]
data[normalized_env_name] = env_value
else:
data[env_name] = env_value

This comment has been minimized.

Copy link
@saikumar1752

saikumar1752 Apr 10, 2024

@zzstoatzz, if env_prefix is passed(present) then any value not starting with env_prefix should not be populated.
Test case should be added for the same scenario.
cc: @samuelcolvin

This comment has been minimized.

Copy link
@hramezani

hramezani Apr 10, 2024

Member

I think some tests cover this part. the test is not in this PR

return data

def __repr__(self) -> str:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2354,3 +2354,16 @@ class Settings(BaseSettings):

s = Settings()
assert s.model_dump() == {'foo': '', 'bar_alias': '0', 'nested_alias': {'a': '1', 'b': '2'}}


def test_dotenv_with_extra_and_env_prefix(tmp_path):
p = tmp_path / '.env'
p.write_text('xxx__foo=1\nxxx__extra_var=extra_value')

class Settings(BaseSettings):
model_config = SettingsConfigDict(extra='allow', env_file=p, env_prefix='xxx__')

foo: str = ''

s = Settings()
assert s.model_dump() == {'foo': '1', 'extra_var': 'extra_value'}

0 comments on commit 859805a

Please sign in to comment.