Skip to content

Commit

Permalink
Migrate 'test_conf.yml' to 'assert_type' tests (#2182)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaeppe authored May 25, 2024
1 parent 1f4efbe commit 95e7d4d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 69 deletions.
4 changes: 2 additions & 2 deletions django-stubs/conf/urls/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ handler403: str | Callable[..., HttpResponse]
handler404: str | Callable[..., HttpResponse]
handler500: str | Callable[..., HttpResponse]

IncludedURLConf: TypeAlias = tuple[Sequence[URLResolver | URLPattern], str | None, str | None]
_IncludedURLConf: TypeAlias = tuple[Sequence[URLResolver | URLPattern], str | None, str | None]

# Deprecated
@overload
Expand All @@ -21,7 +21,7 @@ def url(
) -> URLPattern: ...
@overload
def url(
regex: str, view: IncludedURLConf, kwargs: dict[str, Any] | None = ..., name: str | None = ...
regex: str, view: _IncludedURLConf, kwargs: dict[str, Any] | None = ..., name: str | None = ...
) -> URLResolver: ...
@overload
def url(
Expand Down
12 changes: 6 additions & 6 deletions django-stubs/urls/conf.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ from django.urls import URLPattern, URLResolver, _AnyURL
from django.utils.functional import _StrOrPromise
from typing_extensions import TypeAlias

from ..conf.urls import IncludedURLConf
from ..conf.urls import _IncludedURLConf
from ..http.response import HttpResponseBase

_URLConf: TypeAlias = str | ModuleType | Sequence[_AnyURL]

def include(
arg: _URLConf | tuple[_URLConf, str], namespace: str | None = ...
) -> tuple[Sequence[URLResolver | URLPattern], str | None, str | None]: ...
def include(arg: _URLConf | tuple[_URLConf, str], namespace: str | None = ...) -> _IncludedURLConf: ...

# path()
@overload
Expand All @@ -28,7 +26,9 @@ def path(
name: str = ...,
) -> URLPattern: ...
@overload
def path(route: _StrOrPromise, view: IncludedURLConf, kwargs: dict[str, Any] = ..., name: str = ...) -> URLResolver: ...
def path(
route: _StrOrPromise, view: _IncludedURLConf, kwargs: dict[str, Any] = ..., name: str = ...
) -> URLResolver: ...
@overload
def path(
route: _StrOrPromise, view: Sequence[URLResolver | str], kwargs: dict[str, Any] = ..., name: str = ...
Expand All @@ -48,7 +48,7 @@ def re_path(
) -> URLPattern: ...
@overload
def re_path(
route: _StrOrPromise, view: IncludedURLConf, kwargs: dict[str, Any] = ..., name: str = ...
route: _StrOrPromise, view: _IncludedURLConf, kwargs: dict[str, Any] = ..., name: str = ...
) -> URLResolver: ...
@overload
def re_path(
Expand Down
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ addopts =
--cache-clear
--mypy-ini-file=mypy.ini
--mypy-extension-hook=scripts.tests_extension_hook.django_plugin_hook
--ignore=tests/assert_type
1 change: 0 additions & 1 deletion scripts/stubtest/allowlist_todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ django.conf.LazySettings.DEFAULT_FILE_STORAGE
django.conf.LazySettings.STATICFILES_STORAGE
django.conf.STATICFILES_STORAGE_DEPRECATED_MSG
django.conf.global_settings.gettext_noop
django.conf.urls.IncludedURLConf
django.conf.urls.url
django.contrib.admin.FieldListFilter.title
django.contrib.admin.ModelAdmin
Expand Down
49 changes: 49 additions & 0 deletions tests/assert_type/urls/test_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from typing import List, Tuple

from django.conf.urls.i18n import urlpatterns as i18n_urlpatterns
from django.contrib import admin
from django.contrib.auth.views import LoginView
from django.contrib.flatpages import urls as flatpages_urls
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.http import HttpResponse
from django.urls import URLPattern, URLResolver, _AnyURL, include, path, re_path
from django.utils.translation import gettext_lazy as _
from typing_extensions import assert_type

# Test 'path' accepts mix of pattern and resolver object
include1: Tuple[List[_AnyURL], None, None] = ([], None, None)
assert_type(path("test/", include1), URLResolver)

# Test 'path' accepts pattern resolver union subset
include2: Tuple[List[URLPattern], None, None] = ([], None, None)
assert_type(path("test/", include2), URLResolver)

# Test 'path'
assert_type(path("admin/", admin.site.urls), URLResolver)
assert_type(path(_("admin/"), admin.site.urls, name="admin"), URLResolver)
assert_type(path("login/", LoginView.as_view(), name="login1"), URLPattern)
assert_type(path(_("login/"), LoginView.as_view(), name="login2"), URLPattern)


def v1() -> HttpResponse: ...
async def v2() -> HttpResponse: ...


assert_type(path("v1/", v1), URLPattern)
assert_type(path("v2/", v2), URLPattern)
assert_type(re_path("^v1/", v1), URLPattern)
assert_type(re_path("^v2/", v2), URLPattern)

# Test 'include'
patterns1: List[_AnyURL] = []
assert_type(re_path(_("^foo/"), include(patterns1)), URLResolver)
assert_type(re_path("^foo/", include(patterns1, namespace="foo")), URLResolver)
assert_type(re_path("^foo/", include((patterns1, "foo"), namespace="foo")), URLResolver)
assert_type(re_path("^foo/", include(patterns1, "foo")), URLResolver)
assert_type(path("flat/", include(flatpages_urls)), URLResolver)
assert_type(path("flat/", include((flatpages_urls, "static"))), URLResolver)
assert_type(path("i18n/", include(i18n_urlpatterns)), URLResolver)
assert_type(path("i18n/", include((i18n_urlpatterns, "i18n"))), URLResolver)
assert_type(path("admindocs/", include("django.contrib.admindocs.urls")), URLResolver)
assert_type(path("admindocs/", include(("django.contrib.admindocs.urls", "i18n"))), URLResolver)
assert_type(path("", include(staticfiles_urlpatterns(prefix="static/"))), URLResolver)
60 changes: 0 additions & 60 deletions tests/typecheck/urls/test_conf.yml

This file was deleted.

0 comments on commit 95e7d4d

Please sign in to comment.