Skip to content
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ WARDEN_DATABASE_PASSWORD="secret"
```

> [!NOTE]
> If a key contains an underscore, you need to replace it by a `-` to set it using environment variables.
> E.g. `qpu.retry_max` can be set with the `WARDEN_QPU_RETRY-MAX` environment variable.
> If a key contains an underscore, you need to remove any `_` in its name to set it using environment variables.
> E.g. `qpu.retry_max` can be set with the `WARDEN_QPU_RETRYMAX` environment variable.


The following options are configurable:
Expand Down
16 changes: 8 additions & 8 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ def test_config_env_vars_use_warden_prefix(monkeypatch, tmp_path):
assert config.api.host == "127.0.0.1"


def test_config_env_vars_kebab_case(monkeypatch, tmp_path):
def test_config_env_vars_nocase(monkeypatch, tmp_path):
"""
Test that the parameters with underscores can be set with the kebab-case
env-variable alternative
Test that the parameters with underscores can be set by removing underscores
in the env-variable alternative
"""
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("WARDEN_SCHEDULER_QPU-POLLING-INTERVAL-S", "999")
monkeypatch.setenv("WARDEN_QPU_RETRY-MAX", "999")
monkeypatch.setenv("WARDEN_API_AUTHORIZED-USERS", '[1001, "9999"]')
monkeypatch.setenv("WARDEN_SCHEDULER_QPUPOLLINGINTERVALS", "999")
monkeypatch.setenv("WARDEN_QPU_RETRYMAX", "999")
monkeypatch.setenv("WARDEN_API_AUTHORIZEDUSERS", '[1001, "9999"]')

config = Config()

Expand All @@ -49,8 +49,8 @@ def test_config_env_vars_kebab_case(monkeypatch, tmp_path):

def test_config_parse_lists_from_env(monkeypatch, tmp_path):
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("WARDEN_API_AUTHORIZED-USERS", '[1001, "9999"]')
monkeypatch.setenv("WARDEN_API_ADMIN-USERS", '[1001, "9999"]')
monkeypatch.setenv("WARDEN_API_AUTHORIZEDUSERS", '[1001, "9999"]')
monkeypatch.setenv("WARDEN_API_ADMINUSERS", '[1001, "9999"]')

config = Config()

Expand Down
8 changes: 4 additions & 4 deletions warden/lib/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
API_PREFIX = "/api/v1"


def to_kebab(snake: str) -> str:
return snake.replace("_", "-")
def to_nocase(snake: str) -> str:
return snake.replace("_", "")


class WardenSettings(BaseSettings):
# Give kebab-case aliases to all fields but still allow validating by field name
# Give nocase aliases to all fields but still allow validating by field name
model_config = SettingsConfigDict(
validate_by_name=True,
alias_generator=AliasGenerator(validation_alias=to_kebab),
alias_generator=AliasGenerator(validation_alias=to_nocase),
)


Expand Down
Loading