Skip to content
Open
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
4 changes: 2 additions & 2 deletions ninja/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from typing import (
TYPE_CHECKING,
Any,
Expand All @@ -13,6 +12,7 @@
Union,
)

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

def _validate(self) -> None:
# urls namespacing validation
skip_registry = os.environ.get("NINJA_SKIP_REGISTRY", False)
skip_registry = getattr(settings, "NINJA_SKIP_REGISTRY", False)
if (
not skip_registry
and self.urls_namespace in NinjaAPI._registry
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo.settings")

import django # noqa
from django.conf import settings # noqa

django.setup()


def pytest_generate_tests(metafunc):
os.environ["NINJA_SKIP_REGISTRY"] = "yes"
settings.NINJA_SKIP_REGISTRY = True
12 changes: 4 additions & 8 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import contextlib
import os
from pathlib import Path
from tempfile import NamedTemporaryFile

Expand Down Expand Up @@ -102,10 +101,7 @@ def test_method(method, path, expected_status, expected_data, expected_streaming
assert data == expected_data


def test_validates():
try:
os.environ["NINJA_SKIP_REGISTRY"] = ""
with pytest.raises(ConfigError):
_urls = NinjaAPI().urls
finally:
os.environ["NINJA_SKIP_REGISTRY"] = "yes"
def test_validates(settings):
delattr(settings, "NINJA_SKIP_REGISTRY")
with pytest.raises(ConfigError):
_urls = NinjaAPI().urls
Comment on lines +104 to +107
Copy link
Contributor

Choose a reason for hiding this comment

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

This test differs from the old one by not setting NINJA_SKIP_REGISTRY back.

Copy link
Author

@anentropic anentropic Dec 6, 2025

Choose a reason for hiding this comment

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

The pytest-django settings fixture does that automatically... accepting the fixture behaves a bit like if the test case was wrapped in a context manager

(If it wasn't getting restored then many other tests would fail)