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(settings): stop using Config.env_prefix for secrets in BaseSettings #2190

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2188-PrettyWood.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Stop using `Config.env_prefix` for secrets in `BaseSettings`
2 changes: 1 addition & 1 deletion pydantic/env_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _build_secrets_files(self, _secrets_dir: Union[Path, str, None] = None) -> D
raise SettingsError(f'secrets_dir must reference a directory, not a {path_type(secrets_path)}')

for field in self.__fields__.values():
for env_name in field.field_info.extra['env_names']:
for env_name in {field.name}:
PrettyWood marked this conversation as resolved.
Show resolved Hide resolved
path = secrets_path / env_name
if path.is_file():
secrets[field.alias] = path.read_text().strip()
Expand Down
17 changes: 17 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,3 +852,20 @@ class Config:
secrets_dir = tmp_path

assert Settings(_env_file=e).dict() == {'foo': 'foo_env_value_str'}


def test_mix_env_variables_secrets(tmp_path, env):
p = tmp_path / 'foo'
p.write_text('foo_secret_value_str')

class Settings(BaseSettings):
foo: str # secret
bar: str # env variable

class Config:
env_prefix = 'PIKA_'
secrets_dir = tmp_path

env.set('PIKA_BAR', 'bar_env_value_str')

assert Settings().dict() == {'foo': 'foo_secret_value_str', 'bar': 'bar_env_value_str'}