Improve type hints of URL conf & include() (#949)

* Improve type hints of URL conf & include()

The type of `urlpatterns` list is `List[Union[URLPattern, URLResolver]]`.

* https://docs.djangoproject.com/en/dev/ref/urls/#django.urls.include
* https://docs.djangoproject.com/en/4.0/ref/urls/

* Alias _AnyURL = Union[URLPattern, URLResolver]

* Fix extract_views_from_urlpatterns
This commit is contained in:
Marti Raudsepp
2022-05-06 09:00:21 +03:00
committed by GitHub
parent 4a5d330228
commit 1a36c6c379
11 changed files with 72 additions and 28 deletions

View File

@@ -1,9 +1,9 @@
- case: test_path_accepts_mix_of_pattern_and_resolver_output
main: |
from typing import List, Tuple, Union
from django.urls import path, URLPattern, URLResolver
from django.urls import path, _AnyURL
def include() -> Tuple[List[Union[URLPattern, URLResolver]], None, None]: ...
def include() -> Tuple[List[_AnyURL], None, None]: ...
path('test/', include())
@@ -16,3 +16,31 @@
def include() -> Tuple[List[URLPattern], None, None]: ...
path('test/', include())
- case: test_urlconf_include
main: |
from typing import List
from django.conf.urls.i18n import urlpatterns as i18n_urlpatterns
from django.contrib.auth.views import LoginView
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
from django.contrib.flatpages import urls as flatpages_urls
from django.urls import _AnyURL, re_path, include, path
foo_patterns: List[_AnyURL] = []
urlpatterns: List[_AnyURL] = [
path('login/', LoginView.as_view(), name='login'),
path('admin/', admin.site.urls),
re_path('^foo/', include(foo_patterns, namespace='foo')),
re_path('^foo/', include((foo_patterns, 'foo'), namespace='foo')),
re_path('^foo/', include(foo_patterns, 'foo')),
path('flat/', include(flatpages_urls)),
path('flat/', include((flatpages_urls, 'static'))),
path('i18n/', include(i18n_urlpatterns)),
path('i18n/', include((i18n_urlpatterns, 'i18n'))),
path('admindocs/', include('django.contrib.admindocs.urls')),
path('admindocs/', include(('django.contrib.admindocs.urls', 'i18n'))),
path('', include(staticfiles_urlpatterns(prefix='static/')))
]