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

Migrate 'test_conf.yml' to 'assert_type' tests #2182

Merged
merged 3 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 3 deletions django-stubs/urls/conf.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ 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: ...
flaeppe marked this conversation as resolved.
Show resolved Hide resolved

# path()
@overload
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
3 changes: 3 additions & 0 deletions scripts/stubtest/allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,6 @@ django.contrib.sessions.base_session.AbstractBaseSession.Meta
# Custom __str__ that we don't want to overcomplicate:
django.forms.utils.RenderableMixin.__str__
django.forms.utils.RenderableMixin.__html__

# TypeAlias that does not exist at runtime
django.conf.urls.IncludedURLConf
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.

Loading