Skip to content

Commit

Permalink
Use Sequence instead of List for path param
Browse files Browse the repository at this point in the history
Unlike `List`, which is invariant, `Sequence` is covariant, which lets
`path` accept lists of subsets of the `Union` as well.

I believe this is safe, as django doesn't mutate this input. I found
[this
comment](python/mypy#3351 (comment))
helpful
  • Loading branch information
davidszotten committed Jul 3, 2021
1 parent cf6952c commit 6ea159b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
6 changes: 3 additions & 3 deletions django-stubs/conf/urls/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Stubs for django.conf.urls (Python 3.5)
from typing import Any, Callable, Dict, List, Optional, overload, Tuple, Union
from typing import Any, Callable, Dict, Optional, overload, Sequence, Tuple, Union

from django.http.response import HttpResponse, HttpResponseBase

Expand All @@ -10,7 +10,7 @@ handler403: Union[str, Callable[..., HttpResponse]] = ...
handler404: Union[str, Callable[..., HttpResponse]] = ...
handler500: Union[str, Callable[..., HttpResponse]] = ...

IncludedURLConf = Tuple[List[Union[URLResolver, URLPattern]], Optional[str], Optional[str]]
IncludedURLConf = Tuple[Sequence[Union[URLResolver, URLPattern]], Optional[str], Optional[str]]

def include(arg: Any, namespace: str = ..., app_name: str = ...) -> IncludedURLConf: ...
@overload
Expand All @@ -21,5 +21,5 @@ def url(
def url(regex: str, view: IncludedURLConf, kwargs: Dict[str, Any] = ..., name: str = ...) -> URLResolver: ...
@overload
def url(
regex: str, view: List[Union[URLResolver, str]], kwargs: Dict[str, Any] = ..., name: str = ...
regex: str, view: Sequence[Union[URLResolver, str]], kwargs: Dict[str, Any] = ..., name: str = ...
) -> URLResolver: ...
8 changes: 4 additions & 4 deletions django-stubs/urls/conf.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Any, List, Optional, Tuple, overload, Callable, Dict, Union
from typing import Any, Optional, Sequence, Tuple, overload, Callable, Dict, Union

from .resolvers import URLResolver, URLPattern
from ..conf.urls import IncludedURLConf
from ..http.response import HttpResponseBase

def include(
arg: Any, namespace: Optional[str] = ...
) -> Tuple[List[Union[URLResolver, URLPattern]], Optional[str], Optional[str]]: ...
) -> Tuple[Sequence[Union[URLResolver, URLPattern]], Optional[str], Optional[str]]: ...

# path()
@overload
Expand All @@ -17,7 +17,7 @@ def path(
def path(route: str, view: IncludedURLConf, kwargs: Dict[str, Any] = ..., name: str = ...) -> URLResolver: ...
@overload
def path(
route: str, view: List[Union[URLResolver, str]], kwargs: Dict[str, Any] = ..., name: str = ...
route: str, view: Sequence[Union[URLResolver, str]], kwargs: Dict[str, Any] = ..., name: str = ...
) -> URLResolver: ...

# re_path()
Expand All @@ -29,5 +29,5 @@ def re_path(
def re_path(route: str, view: IncludedURLConf, kwargs: Dict[str, Any] = ..., name: str = ...) -> URLResolver: ...
@overload
def re_path(
route: str, view: List[Union[URLResolver, str]], kwargs: Dict[str, Any] = ..., name: str = ...
route: str, view: Sequence[Union[URLResolver, str]], kwargs: Dict[str, Any] = ..., name: str = ...
) -> URLResolver: ...
10 changes: 10 additions & 0 deletions tests/typecheck/urls/test_conf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@
def include() -> Tuple[List[Union[URLPattern, URLResolver]], None, None]: ...
path('test/', include())
- case: test_path_accepts_pattern_resolver_union_subset
main: |
from typing import List, Tuple
from django.urls import path, URLPattern
def include() -> Tuple[List[URLPattern], None, None]: ...
path('test/', include())

0 comments on commit 6ea159b

Please sign in to comment.