Skip to content

Commit

Permalink
Fix ignored _secrets_dir arg of BaseSettings (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
makukha authored Sep 3, 2024
1 parent e5fe409 commit 971cd01
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.bumpversion]
current_version = "0.2.1"
current_version = "0.3.0"
allow_dirty = true
files = [
{filename = "src/pydantic_file_secrets/__version__.py"},
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
> Use file secrets in nested [Pydantic Settings](https://docs.pydantic.dev/latest/concepts/pydantic_settings/) models, drop-in replacement for `SecretsSettingsSource`.
![GitHub License](https://img.shields.io/github/license/makukha/pydantic-file-secrets)
[![Tests](https://raw.githubusercontent.com/makukha/pydantic-file-secrets/0.2.1/docs/badge/tests.svg)](https://github.com/makukha/pydantic-file-secrets)
[![Coverage](https://raw.githubusercontent.com/makukha/pydantic-file-secrets/0.2.1/docs/badge/coverage.svg)](https://github.com/makukha/pydantic-file-secrets)
[![Tests](https://raw.githubusercontent.com/makukha/pydantic-file-secrets/0.3.0/docs/badge/tests.svg)](https://github.com/makukha/pydantic-file-secrets)
[![Coverage](https://raw.githubusercontent.com/makukha/pydantic-file-secrets/0.3.0/docs/badge/coverage.svg)](https://github.com/makukha/pydantic-file-secrets)
[![linting - Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v1.json)](https://github.com/astral-sh/ruff)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) \
[![pypi](https://img.shields.io/pypi/v/pydantic-file-secrets.svg#0.2.1)](https://pypi.python.org/pypi/pydantic-file-secrets)
[![pypi](https://img.shields.io/pypi/v/pydantic-file-secrets.svg#0.3.0)](https://pypi.python.org/pypi/pydantic-file-secrets)
[![versions](https://img.shields.io/pypi/pyversions/pydantic-file-secrets.svg)](https://pypi.org/project/pydantic-file-secrets)
[![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://pydantic.dev)

Expand Down Expand Up @@ -102,9 +102,10 @@ class Settings(BaseSettings):
file_secret_settings,
):
return (
env_settings,
init_settings,
FileSecretsSettingsSource(settings_cls),
env_settings,
dotenv_settings,
FileSecretsSettingsSource(file_secret_settings),
)

```
Expand Down
14 changes: 7 additions & 7 deletions docs/badge/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions docs/badge/tests.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 12 additions & 2 deletions src/pydantic_file_secrets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Literal
import warnings

from pydantic_settings import BaseSettings, EnvSettingsSource, SettingsError
from pydantic_settings import BaseSettings, EnvSettingsSource, SecretsSettingsSource, SettingsError
from pydantic_settings.sources import parse_env_vars
from pydantic_settings.utils import path_type_label

Expand All @@ -20,7 +20,7 @@
class FileSecretsSettingsSource(EnvSettingsSource):
def __init__(
self,
settings_cls: type[BaseSettings],
file_secret_settings: SecretsSettingsSource | BaseSettings,
secrets_dir: str | Path | list[str | Path] | None = None,
secrets_dir_missing: SecretsDirMissing | None = None,
secrets_dir_max_size: int | None = None,
Expand All @@ -29,9 +29,19 @@ def __init__(
secrets_nested_delimiter: str | None = None,
secrets_nested_subdir: bool | None = None,
) -> None:
if isinstance(file_secret_settings, BaseSettings):
# We allow the first argument to be settings_cls like original
# SecretsSettingsSource. However, it is recommended to pass
# SecretsSettingsSource instance instead (as it is shown in usage examples),
# otherwise `_secrets_dir` arg passed to Settings() constructor
# will be ignored.
settings_cls = file_secret_settings
else:
settings_cls = file_secret_settings.settings_cls
# config options
conf = settings_cls.model_config
self.secrets_dir: str | Path | list[str | Path] | None = first_not_none(
getattr(file_secret_settings, 'secrets_dir', None),
secrets_dir,
conf.get('secrets_dir'),
)
Expand Down
2 changes: 1 addition & 1 deletion src/pydantic_file_secrets/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.2.1'
__version__ = '0.3.0'
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def settings_customise_sources(
file_secret_settings,
) -> tuple:
return (
env_settings,
init_settings,
FileSecretsSettingsSource(settings_cls),
env_settings,
FileSecretsSettingsSource(file_secret_settings),
)

TestSettings.model_config = model_config or {}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_ignored_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def settings_customise_sources(
file_secret_settings,
) -> tuple:
return (
env_settings,
init_settings,
FileSecretsSettingsSource(settings_cls),
env_settings,
FileSecretsSettingsSource(file_secret_settings),
)

SettingsSSS.model_config = model_config
Expand Down
15 changes: 15 additions & 0 deletions tests/test_settings_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def test_strip_whitespace(settings_model, monkeypatch, secrets_dir):
monkeypatch.setenv('DB__USER', 'user')
secrets_dir.add_files(
('dir1/app_key', 'secret1'),
('dir2/db___password', 'secret2'),
)
Settings = settings_model(
model_config=dict(
env_nested_delimiter='__',
secrets_nested_delimiter='___',
),
)
conf = Settings(_secrets_dir=[secrets_dir / 'dir1', secrets_dir / 'dir2'])
assert conf.app_key == 'secret1'
assert conf.db.password == 'secret2' # noqa: S105
4 changes: 2 additions & 2 deletions tests/test_unimportant.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydantic_file_secrets import FileSecretsSettingsSource
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic_settings import BaseSettings, SecretsSettingsSource, SettingsConfigDict


def test_repr(secrets_dir):
Expand All @@ -8,5 +8,5 @@ class Settings(BaseSettings):
secrets_dir=secrets_dir,
)

src = FileSecretsSettingsSource(Settings)
src = FileSecretsSettingsSource(SecretsSettingsSource(Settings))
assert f'{src!r}'.startswith(f'{src.__class__.__name__}(')

0 comments on commit 971cd01

Please sign in to comment.