Skip to content

Commit 3d7a572

Browse files
committed
Improve typing of view decorators
A new type variable is defined, matching (async) callables taking a request and returning a response. Some decorators are defined as views to comply with pyright
1 parent cb73083 commit 3d7a572

File tree

9 files changed

+40
-53
lines changed

9 files changed

+40
-53
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from typing import TypeVar
2+
3+
from django.utils.deprecation import _AsyncGetResponseCallable, _GetResponseCallable
4+
5+
_ViewFuncT = TypeVar("_ViewFuncT", bound=_AsyncGetResponseCallable | _GetResponseCallable) # noqa: PYI018
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from collections.abc import Callable
2-
from typing import Any, TypeVar
2+
from typing import Any
33

4-
_F = TypeVar("_F", bound=Callable[..., Any])
4+
from . import _ViewFuncT
55

66
def cache_page(
77
timeout: float | None, *, cache: Any | None = ..., key_prefix: Any | None = ...
8-
) -> Callable[[_F], _F]: ...
9-
def cache_control(**kwargs: Any) -> Callable[[_F], _F]: ...
10-
def never_cache(view_func: _F) -> _F: ...
8+
) -> Callable[[_ViewFuncT], _ViewFuncT]: ...
9+
def cache_control(**kwargs: Any) -> Callable[[_ViewFuncT], _ViewFuncT]: ...
10+
def never_cache(view_func: _ViewFuncT, /) -> _ViewFuncT: ...
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
from collections.abc import Callable
2-
from typing import Any, TypeVar
1+
from . import _ViewFuncT
32

4-
_F = TypeVar("_F", bound=Callable[..., Any])
5-
6-
def xframe_options_deny(view_func: _F) -> _F: ...
7-
def xframe_options_sameorigin(view_func: _F) -> _F: ...
8-
def xframe_options_exempt(view_func: _F) -> _F: ...
3+
def xframe_options_deny(view_func: _ViewFuncT, /) -> _ViewFuncT: ...
4+
def xframe_options_sameorigin(view_func: _ViewFuncT, /) -> _ViewFuncT: ...
5+
def xframe_options_exempt(view_func: _ViewFuncT, /) -> _ViewFuncT: ...
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from collections.abc import Callable
2-
from typing import Any, TypeVar
1+
from . import _ViewFuncT
32

4-
_C = TypeVar("_C", bound=Callable[..., Any])
5-
6-
def no_append_slash(view_func: _C) -> _C: ...
3+
def no_append_slash(view_func: _ViewFuncT, /) -> _ViewFuncT: ...
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
from collections.abc import Callable
2-
from typing import Any, TypeVar
3-
41
from django.middleware.csrf import CsrfViewMiddleware
52

6-
csrf_protect: Callable[[_F], _F]
3+
from . import _ViewFuncT
4+
5+
def csrf_protect(view_func: _ViewFuncT, /) -> _ViewFuncT: ...
76

87
class _EnsureCsrfToken(CsrfViewMiddleware): ...
98

10-
requires_csrf_token: Callable[[_F], _F]
9+
def requires_csrf_token(view_func: _ViewFuncT, /) -> _ViewFuncT: ...
1110

1211
class _EnsureCsrfCookie(CsrfViewMiddleware): ...
1312

14-
ensure_csrf_cookie: Callable[[_F], _F]
15-
16-
_F = TypeVar("_F", bound=Callable[..., Any])
17-
18-
def csrf_exempt(view_func: _F) -> _F: ...
13+
def ensure_csrf_cookie(view_func: _ViewFuncT, /) -> _ViewFuncT: ...
14+
def csrf_exempt(view_func: _ViewFuncT, /) -> _ViewFuncT: ...
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from collections.abc import Callable
2-
from typing import Any, Literal, TypeVar
2+
from typing import Literal
33

4-
_F = TypeVar("_F", bound=Callable[..., Any])
4+
from . import _ViewFuncT
55

66
coroutine_functions_to_sensitive_variables: dict[int, Literal["__ALL__"] | tuple[str, ...]]
77

8-
def sensitive_variables(*variables: str) -> Callable[[_F], _F]: ...
9-
def sensitive_post_parameters(*parameters: str) -> Callable[[_F], _F]: ...
8+
def sensitive_variables(*variables: str) -> Callable[[_ViewFuncT], _ViewFuncT]: ...
9+
def sensitive_post_parameters(*parameters: str) -> Callable[[_ViewFuncT], _ViewFuncT]: ...
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from collections.abc import Callable
2-
from typing import Any, TypeVar
1+
from . import _ViewFuncT
32

4-
_C = TypeVar("_C", bound=Callable[..., Any])
5-
6-
gzip_page: Callable[[_C], _C]
3+
def gzip_page(view_func: _ViewFuncT, /) -> _ViewFuncT: ...
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
from collections.abc import Callable, Container
22
from datetime import datetime
3-
from typing import Any, TypeVar
43

5-
_F = TypeVar("_F", bound=Callable[..., Any])
6-
7-
conditional_page: Callable[[_F], _F]
8-
9-
def require_http_methods(request_method_list: Container[str]) -> Callable[[_F], _F]: ...
10-
11-
require_GET: Callable[[_F], _F]
12-
require_POST: Callable[[_F], _F]
13-
require_safe: Callable[[_F], _F]
4+
from . import _ViewFuncT
145

6+
def conditional_page(view_func: _ViewFuncT, /) -> _ViewFuncT: ...
7+
def require_http_methods(request_method_list: Container[str]) -> Callable[[_ViewFuncT], _ViewFuncT]: ...
8+
def require_GET(func: _ViewFuncT, /) -> _ViewFuncT: ...
9+
def require_POST(func: _ViewFuncT, /) -> _ViewFuncT: ...
10+
def require_safe(func: _ViewFuncT, /) -> _ViewFuncT: ...
1511
def condition(
1612
etag_func: Callable[..., str | None] | None = ..., last_modified_func: Callable[..., datetime | None] | None = ...
17-
) -> Callable[[_F], _F]: ...
18-
def etag(etag_func: Callable[..., str | None]) -> Callable[[_F], _F]: ...
19-
def last_modified(last_modified_func: Callable[..., datetime | None]) -> Callable[[_F], _F]: ...
13+
) -> Callable[[_ViewFuncT], _ViewFuncT]: ...
14+
def etag(etag_func: Callable[..., str | None]) -> Callable[[_ViewFuncT], _ViewFuncT]: ...
15+
def last_modified(last_modified_func: Callable[..., datetime | None]) -> Callable[[_ViewFuncT], _ViewFuncT]: ...
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from collections.abc import Callable
2-
from typing import Any, TypeVar
32

4-
_F = TypeVar("_F", bound=Callable[..., Any])
3+
from . import _ViewFuncT
54

6-
def vary_on_headers(*headers: str) -> Callable[[_F], _F]: ...
7-
def vary_on_cookie(func: _F) -> _F: ...
5+
def vary_on_headers(*headers: str) -> Callable[[_ViewFuncT], _ViewFuncT]: ...
6+
def vary_on_cookie(func: _ViewFuncT, /) -> _ViewFuncT: ...

0 commit comments

Comments
 (0)