Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typechecking on API URLs (mostly error suppression) #4915

Merged
merged 1 commit into from
Dec 18, 2024
Merged
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
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it good that we are using Django's private API here?

Copy link
Member Author

Choose a reason for hiding this comment

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

this is not actually private API, but types defined by django-stubs! this import does not exist at runtime, and that's why it's guarded by the if TYPE_CHECKING: in combination with from __future__ import annotations



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,
)
Loading