Skip to content

Commit

Permalink
🏷️ Suppress type errors on decorator_include
Browse files Browse the repository at this point in the history
It's doing a lot of duck-typing and does not play nice with the types
defined in django-stubs, which would ideally be protocols rather than
named types for these kind of things.

However, we let the decorator_include declare the type it wants in
the path arguments, and apply a cast around the custom class to
silence the remaining type checker warnings.

The most important thing here is that the input types for decorator_include
are checked and our urls.py modules aren't unusable.
  • Loading branch information
sergei-maertens committed Dec 17, 2024
1 parent 486d9cb commit 07ef1ca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions pyright.pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[tool.pyright]
include = [
"src/openforms/api/urls",
# plugin/registry-related code (base classes etc.)
"src/openforms/plugins",
"src/openforms/appointments/base.py",
Expand Down
17 changes: 14 additions & 3 deletions src/openforms/utils/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

from collections.abc import Sequence
from importlib import import_module
from typing import Any, Callable
from typing import TYPE_CHECKING, Any, Callable, cast
from urllib.parse import urljoin, urlsplit

from django.conf import settings
Expand All @@ -13,6 +14,9 @@

from openforms.typing import AnyRequest

if TYPE_CHECKING:
from django.conf.urls import _IncludedURLConf


def build_absolute_uri(location: str, request: AnyRequest | None = None):
"""
Expand Down Expand Up @@ -204,7 +208,7 @@ def decorator_include(
decorators: Callable[..., Any] | list[Callable[..., Any]],
arg: Any,
namespace: str | None = None,
) -> tuple[_DecoratedPatterns, str | None, str | None]:
) -> _IncludedURLConf:
"""
Works like ``django.conf.urls.include`` but takes a view decorator
or a list of view decorators as the first argument and applies them,
Expand All @@ -218,4 +222,11 @@ def decorator_include(
urlconf_module, app_name, namespace = arg
else:
urlconf_module, app_name, namespace = include(arg, namespace=namespace)
return _DecoratedPatterns(urlconf_module, decorators), app_name, namespace
return (
cast(
Sequence[URLPattern | URLResolver],
_DecoratedPatterns(urlconf_module, decorators),
),
app_name,
namespace,
)

0 comments on commit 07ef1ca

Please sign in to comment.