Skip to content

Commit a9eee99

Browse files
authored
Allow subclasses of APIView to override get_permissions with return type implementing _SupportsHasPermission (#320)
Issue is that list is invariant, which makes the protocol not work. Changing it so a sequence of course breaks stuff that relies on the result being mutable, but I am assuming that that should not be supported anyways.
1 parent cdd962e commit a9eee99

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

rest_framework-stubs/views.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class APIView(View):
7171
def get_renderers(self) -> list[BaseRenderer]: ...
7272
def get_parsers(self) -> list[BaseParser]: ...
7373
def get_authenticators(self) -> list[BaseAuthentication]: ...
74-
def get_permissions(self) -> list[_SupportsHasPermission]: ...
74+
def get_permissions(self) -> Sequence[_SupportsHasPermission]: ...
7575
def get_throttles(self) -> list[BaseThrottle]: ...
7676
def get_content_negotiator(self) -> BaseContentNegotiation: ...
7777
def get_exception_handler(self) -> Callable: ...

tests/typecheck/test_views.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,14 @@
5555
pass
5656
5757
reveal_type(MyRetrieveViewSet().get_object()) # N: Revealed type is "main.MyModel"
58+
59+
- case: test_override_get_permissions
60+
main: |
61+
from typing import List
62+
63+
from rest_framework.viewsets import GenericViewSet
64+
from rest_framework.permissions import BasePermission
65+
66+
class MyView(GenericViewSet):
67+
def get_permissions(self) -> List[BasePermission]:
68+
...

0 commit comments

Comments
 (0)