Skip to content

Commit 24d7af1

Browse files
committed
Validate values of non-settings environment variables
1 parent 933ed5a commit 24d7af1

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

baybe/settings.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,17 @@
4242
"to 'False'/'Auto'."
4343
)
4444

45-
_ENV_VARS_WHITELIST = {
46-
"BAYBE_TEST_ENV", # defines testing scope
47-
}
48-
"""The collection of whitelisted **additional** environment variables allowed."""
45+
46+
def _validate_whitelist_env_vars(vars: dict[str, str], /) -> None:
47+
"""Validate the values of non-settings environment variables."""
48+
if (value := vars.pop("BAYBE_TEST_ENV", None)) is not None:
49+
if value not in {"CORETEST", "FULLTEST", "GPUTEST"}:
50+
raise ValueError(
51+
f"Allowed values for 'BAYBE_TEST_ENV' are "
52+
f"'CORETEST', 'FULLTEST', and 'GPUTEST'. Given: '{value}'"
53+
)
54+
if vars:
55+
raise RuntimeError(f"Unknown environment variables: {set(vars)}")
4956

5057

5158
class _SlottedContextDecorator:
@@ -267,13 +274,16 @@ def __attrs_pre_init__(self) -> None:
267274
os.environ[f"BAYBE_{(fld.alias or fld.name).upper()}"] = value
268275
# <<<<< Deprecation
269276

270-
env_vars = {name for name in os.environ if name.startswith("BAYBE_")}
271-
unknown = env_vars - (
272-
{f"BAYBE_{attr.alias.upper()}" for attr in self._settings_attributes}
273-
| _ENV_VARS_WHITELIST
277+
known_env_vars = {
278+
f"BAYBE_{attr.alias.upper()}" for attr in self._settings_attributes
279+
}
280+
_validate_whitelist_env_vars(
281+
{
282+
k: v
283+
for k, v in os.environ.items()
284+
if k.startswith("BAYBE_") and k not in known_env_vars
285+
}
274286
)
275-
if unknown:
276-
raise RuntimeError(f"Unknown environment variables: {unknown}")
277287

278288
def __enter__(self) -> Settings:
279289
self.activate()

0 commit comments

Comments
 (0)