Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd5c464

Browse files
committed
🐛 [#201] Fix warning and error in absence of an optional extra
1 parent e0b2bbb commit cd5c464

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

open_api_framework/conf/utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging # noqa: TID251
2+
import os
23
import sys
34
from dataclasses import dataclass
45
from importlib.util import find_spec
@@ -93,25 +94,26 @@ def document():
9394
if default is not undefined and default is not None:
9495
kwargs.setdefault("cast", type(default))
9596

96-
value = _config(option, default=default, *args, **kwargs)
97-
9897
match add_to_docs:
9998
case str(module) if find_spec(module):
10099
document()
101100
case str(module):
102-
if value is not default:
101+
# not installed
102+
if option in os.environ:
103103
warn(
104104
f"{variable.name} found, but required {add_to_docs} is not installed",
105105
RuntimeWarning,
106106
)
107+
if default is undefined:
108+
return default # don't call _config it will require variable.name!
107109
case True:
108110
document()
109111
case False:
110112
pass
111113
case _:
112114
assert_never(add_to_docs)
113115

114-
return value # type: ignore
116+
return _config(option, default=default, *args, **kwargs) # type: ignore
115117

116118

117119
def importable(*items: str) -> list[str]:

tests/test_config_helpers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import itertools as it
12
import os
23

34
import pytest
@@ -6,6 +7,7 @@
67
ENVVAR_REGISTRY,
78
config,
89
get_django_project_dir,
10+
undefined,
911
)
1012

1113

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

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

4346

47+
@pytest.mark.parametrize(
48+
"default,split",
49+
it.product([None, "value", "", undefined], [False, True]),
50+
)
51+
def test_it_doesnt_warn_if_env_is_not_set(default, split):
52+
with pytest.WarningsRecorder() as warnings:
53+
config("FOO_TEST_ENVVAR", default=default, add_to_docs="foo_module", split=True)
54+
assert not warnings.list
55+
56+
4457
def test_get_django_project_dir():
4558
project_path = get_django_project_dir()
4659
assert project_path.parts[-1] == "testapp"

0 commit comments

Comments
 (0)