Skip to content
Merged
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
10 changes: 6 additions & 4 deletions open_api_framework/conf/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging # noqa: TID251
import os
import sys
from dataclasses import dataclass
from importlib.util import find_spec
Expand Down Expand Up @@ -93,25 +94,26 @@ def document():
if default is not undefined and default is not None:
kwargs.setdefault("cast", type(default))

value = _config(option, default=default, *args, **kwargs)

match add_to_docs:
case str(module) if find_spec(module):
document()
case str(module):
if value is not default:
# not installed
if option in os.environ:
warn(
f"{variable.name} found, but required {add_to_docs} is not installed",
RuntimeWarning,
)
if default is undefined:
return default # don't call _config it will require variable.name!
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, returning undefined is scuffed. But I don't want to spend more time on fixing the type annotation, since maykin-common has a decent one. And an open ticket to merge missing functionality in there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh.. That ticket was close, but there's still a draft PR: maykinmedia/django-common#34

case True:
document()
case False:
pass
case _:
assert_never(add_to_docs)

return value # type: ignore
return _config(option, default=default, *args, **kwargs) # type: ignore


def importable(*items: str) -> list[str]:
Expand Down
15 changes: 14 additions & 1 deletion tests/test_config_helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools as it
import os

import pytest
Expand All @@ -6,6 +7,7 @@
ENVVAR_REGISTRY,
config,
get_django_project_dir,
undefined,
)


Expand Down Expand Up @@ -34,13 +36,24 @@ def test_it_raises_warning_if_add_to_docs_module_is_not_present(monkeypatch):
with pytest.warns() as warnings:
value = config("FOO_TEST_ENVVAR", default="value", add_to_docs="foo_module")
assert value == "value"
assert not any(var.name == "FOO_TEST_VAR" for var in ENVVAR_REGISTRY)
# not registered to document
assert not any(var.name == "FOO_TEST_ENVAR" for var in ENVVAR_REGISTRY)

# warning mentions key actionable info
assert "FOO_TEST_ENVVAR" in str(warnings[0])
assert "foo_module" in str(warnings[0])


@pytest.mark.parametrize(
"default,split",
it.product([None, "value", "", undefined], [False, True]),
)
def test_it_doesnt_warn_if_env_is_not_set(default, split):
with pytest.WarningsRecorder() as warnings:
config("FOO_TEST_ENVVAR", default=default, add_to_docs="foo_module", split=True)
assert not warnings.list


def test_get_django_project_dir():
project_path = get_django_project_dir()
assert project_path.parts[-1] == "testapp"
Expand Down