Skip to content

Commit 4957d96

Browse files
committed
fix(action): add support of http.HTTPMethod to action decorator
Signed-off-by: Sergei Shishov <[email protected]>
1 parent a46556c commit 4957d96

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

rest_framework-stubs/decorators.pyi

+12-30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections.abc import Callable, Mapping, Sequence
2+
from http import HTTPMethod
23
from typing import Any, Literal, Protocol, TypeVar
34

45
from django.http import HttpRequest
@@ -29,43 +30,24 @@ class MethodMapper(dict):
2930
def options(self, func: _View) -> _View: ...
3031
def trace(self, func: _View) -> _View: ...
3132

32-
_LOWER_CASE_HTTP_VERBS: TypeAlias = Sequence[
33-
Literal[
34-
"get",
35-
"post",
36-
"delete",
37-
"put",
38-
"patch",
39-
"trace",
40-
"head",
41-
"options",
42-
]
43-
]
44-
4533
_MIXED_CASE_HTTP_VERBS: TypeAlias = Sequence[
34+
# fmt: off
4635
Literal[
47-
"GET",
48-
"POST",
49-
"DELETE",
50-
"PUT",
51-
"PATCH",
52-
"TRACE",
53-
"HEAD",
54-
"OPTIONS",
55-
"get",
56-
"post",
57-
"delete",
58-
"put",
59-
"patch",
60-
"trace",
61-
"head",
62-
"options",
36+
"get" , "GET" , HTTPMethod.GET ,
37+
"post" , "POST" , HTTPMethod.POST ,
38+
"delete" , "DELETE" , HTTPMethod.DELETE ,
39+
"put" , "PUT" , HTTPMethod.PUT ,
40+
"patch" , "PATCH" , HTTPMethod.PATCH ,
41+
"trace" , "TRACE" , HTTPMethod.TRACE ,
42+
"head" , "HEAD" , HTTPMethod.HEAD ,
43+
"options", "OPTIONS", HTTPMethod.OPTIONS,
6344
]
45+
# fmt: on
6446
]
6547

6648
class ViewSetAction(Protocol[_View]):
6749
detail: bool
68-
methods: _LOWER_CASE_HTTP_VERBS
50+
methods: _MIXED_CASE_HTTP_VERBS
6951
url_path: str
7052
url_name: str
7153
kwargs: Mapping[str, Any]

tests/typecheck/test_decorators.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
5050
- case: method_decorator
5151
main: |
52+
from http import HTTPMethod
53+
5254
from rest_framework import viewsets
5355
from rest_framework.decorators import action
5456
from rest_framework.request import Request
@@ -59,5 +61,11 @@
5961
@action(methods=("get",), detail=False)
6062
def view_func_1(self, request: Request) -> Response: ...
6163
62-
@action(methods=["post",], detail=False)
64+
@action(methods=["post"], detail=False)
6365
def view_func_2(self, request: Request) -> Response: ...
66+
67+
@action(methods=("GET",), detail=False)
68+
def view_func_3(self, request: Request) -> Response: ...
69+
70+
@action(methods=[HTTPMethod.POST], detail=False)
71+
def view_func_4(self, request: Request) -> Response: ...

0 commit comments

Comments
 (0)