Skip to content

Commit ccbb5ad

Browse files
anentropicifm-pgarner
authored andcommitted
Allow to disable unnecessary API registry checks via Django settings.NINJA_SKIP_REGISTRY
1 parent 9b7c4f1 commit ccbb5ad

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

ninja/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
from typing import (
32
TYPE_CHECKING,
43
Any,
@@ -13,6 +12,7 @@
1312
Union,
1413
)
1514

15+
from django.conf import settings
1616
from django.http import HttpRequest, HttpResponse
1717
from django.urls import URLPattern, URLResolver, reverse
1818
from django.utils.module_loading import import_string
@@ -561,7 +561,7 @@ def _lookup_exception_handler(self, exc: Exc[_E]) -> Optional[ExcHandler[_E]]:
561561

562562
def _validate(self) -> None:
563563
# urls namespacing validation
564-
skip_registry = os.environ.get("NINJA_SKIP_REGISTRY", False)
564+
skip_registry = getattr(settings, "NINJA_SKIP_REGISTRY", False)
565565
if (
566566
not skip_registry
567567
and self.urls_namespace in NinjaAPI._registry

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo.settings")
1111

1212
import django # noqa
13+
from django.conf import settings # noqa
1314

1415
django.setup()
1516

1617

1718
def pytest_generate_tests(metafunc):
18-
os.environ["NINJA_SKIP_REGISTRY"] = "yes"
19+
settings.NINJA_SKIP_REGISTRY = True

tests/test_app.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import contextlib
2-
import os
32
from pathlib import Path
43
from tempfile import NamedTemporaryFile
54

@@ -102,10 +101,7 @@ def test_method(method, path, expected_status, expected_data, expected_streaming
102101
assert data == expected_data
103102

104103

105-
def test_validates():
106-
try:
107-
os.environ["NINJA_SKIP_REGISTRY"] = ""
108-
with pytest.raises(ConfigError):
109-
_urls = NinjaAPI().urls
110-
finally:
111-
os.environ["NINJA_SKIP_REGISTRY"] = "yes"
104+
def test_validates(settings):
105+
delattr(settings, "NINJA_SKIP_REGISTRY")
106+
with pytest.raises(ConfigError):
107+
_urls = NinjaAPI().urls

0 commit comments

Comments
 (0)