From 07ef1ca708bc70c912686d258b632ec29e8ff222 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Thu, 12 Dec 2024 23:25:39 +0100 Subject: [PATCH] :label: Suppress type errors on decorator_include 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. --- pyright.pyproject.toml | 1 + src/openforms/utils/urls.py | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pyright.pyproject.toml b/pyright.pyproject.toml index b02a0744c1..e69115a161 100644 --- a/pyright.pyproject.toml +++ b/pyright.pyproject.toml @@ -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", diff --git a/src/openforms/utils/urls.py b/src/openforms/utils/urls.py index 10b0c0cb1f..482a7a52f8 100644 --- a/src/openforms/utils/urls.py +++ b/src/openforms/utils/urls.py @@ -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 @@ -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): """ @@ -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, @@ -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, + )