From cd5c464ac0bb028be5493ad8686cb85da46684ec Mon Sep 17 00:00:00 2001 From: Chris Wesseling Date: Fri, 30 Jan 2026 10:27:03 +0100 Subject: [PATCH] :bug: [#201] Fix warning and error in absence of an optional extra --- open_api_framework/conf/utils.py | 10 ++++++---- tests/test_config_helpers.py | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/open_api_framework/conf/utils.py b/open_api_framework/conf/utils.py index 4f54636..fb4e75f 100644 --- a/open_api_framework/conf/utils.py +++ b/open_api_framework/conf/utils.py @@ -1,4 +1,5 @@ import logging # noqa: TID251 +import os import sys from dataclasses import dataclass from importlib.util import find_spec @@ -93,17 +94,18 @@ 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! case True: document() case False: @@ -111,7 +113,7 @@ def document(): 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]: diff --git a/tests/test_config_helpers.py b/tests/test_config_helpers.py index dc0f793..769400e 100644 --- a/tests/test_config_helpers.py +++ b/tests/test_config_helpers.py @@ -1,3 +1,4 @@ +import itertools as it import os import pytest @@ -6,6 +7,7 @@ ENVVAR_REGISTRY, config, get_django_project_dir, + undefined, ) @@ -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"