From 5977c99bdb6131dc03d803fb259707569d510d1a Mon Sep 17 00:00:00 2001 From: Matthias Schoettle Date: Mon, 23 Oct 2023 15:49:24 -0400 Subject: [PATCH 1/2] fix: return list instead of sequence --- rest_framework-stubs/permissions.pyi | 2 +- rest_framework-stubs/views.pyi | 2 +- tests/typecheck/test_views.yml | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/rest_framework-stubs/permissions.pyi b/rest_framework-stubs/permissions.pyi index a4d6e2f92..26c3e2c7e 100644 --- a/rest_framework-stubs/permissions.pyi +++ b/rest_framework-stubs/permissions.pyi @@ -16,7 +16,7 @@ class _SupportsHasPermission(Protocol): # https://github.com/python/mypy/issues/12392 _PermissionClass: TypeAlias = type[BasePermission] | OperandHolder | SingleOperandHolder -class OperationHolderMixin: +class OperationHolderMixin(_SupportsHasPermission): def __and__(self, other: _PermissionClass) -> OperandHolder: ... def __or__(self, other: _PermissionClass) -> OperandHolder: ... def __rand__(self, other: _PermissionClass) -> OperandHolder: ... diff --git a/rest_framework-stubs/views.pyi b/rest_framework-stubs/views.pyi index 5c9d8e50e..d24e73f00 100644 --- a/rest_framework-stubs/views.pyi +++ b/rest_framework-stubs/views.pyi @@ -71,7 +71,7 @@ class APIView(View): def get_renderers(self) -> list[BaseRenderer]: ... def get_parsers(self) -> list[BaseParser]: ... def get_authenticators(self) -> list[BaseAuthentication]: ... - def get_permissions(self) -> Sequence[_SupportsHasPermission]: ... + def get_permissions(self) -> list[_SupportsHasPermission]: ... def get_throttles(self) -> list[BaseThrottle]: ... def get_content_negotiator(self) -> BaseContentNegotiation: ... def get_exception_handler(self) -> Callable: ... diff --git a/tests/typecheck/test_views.yml b/tests/typecheck/test_views.yml index bce6341c8..57320f2a8 100644 --- a/tests/typecheck/test_views.yml +++ b/tests/typecheck/test_views.yml @@ -70,8 +70,19 @@ from typing import List from rest_framework.viewsets import GenericViewSet - from rest_framework.permissions import BasePermission + from rest_framework.permissions import _SupportsHasPermission class MyView(GenericViewSet): - def get_permissions(self) -> List[BasePermission]: - ... + def get_permissions(self) -> List[_SupportsHasPermission]: + return super().get_permissions() + +- case: test_override_get_permissions_operandholder + main: | + from typing import Sequence + + from rest_framework.viewsets import GenericViewSet + from rest_framework.permissions import BasePermission, IsAuthenticated, IsAdminUser, _SupportsHasPermission + + class MyView(GenericViewSet): + def get_permissions(self) -> list[_SupportsHasPermission]: + return [IsAuthenticated & IsAdminUser] From d8bee67c2fe6e61b44894b4978ee1b81137d382f Mon Sep 17 00:00:00 2001 From: Matthias Schoettle Date: Mon, 23 Oct 2023 16:52:29 -0400 Subject: [PATCH 2/2] support more cases --- rest_framework-stubs/permissions.pyi | 2 +- rest_framework-stubs/views.pyi | 2 +- tests/typecheck/test_views.yml | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/rest_framework-stubs/permissions.pyi b/rest_framework-stubs/permissions.pyi index 26c3e2c7e..a4d6e2f92 100644 --- a/rest_framework-stubs/permissions.pyi +++ b/rest_framework-stubs/permissions.pyi @@ -16,7 +16,7 @@ class _SupportsHasPermission(Protocol): # https://github.com/python/mypy/issues/12392 _PermissionClass: TypeAlias = type[BasePermission] | OperandHolder | SingleOperandHolder -class OperationHolderMixin(_SupportsHasPermission): +class OperationHolderMixin: def __and__(self, other: _PermissionClass) -> OperandHolder: ... def __or__(self, other: _PermissionClass) -> OperandHolder: ... def __rand__(self, other: _PermissionClass) -> OperandHolder: ... diff --git a/rest_framework-stubs/views.pyi b/rest_framework-stubs/views.pyi index d24e73f00..5c9d8e50e 100644 --- a/rest_framework-stubs/views.pyi +++ b/rest_framework-stubs/views.pyi @@ -71,7 +71,7 @@ class APIView(View): def get_renderers(self) -> list[BaseRenderer]: ... def get_parsers(self) -> list[BaseParser]: ... def get_authenticators(self) -> list[BaseAuthentication]: ... - def get_permissions(self) -> list[_SupportsHasPermission]: ... + def get_permissions(self) -> Sequence[_SupportsHasPermission]: ... def get_throttles(self) -> list[BaseThrottle]: ... def get_content_negotiator(self) -> BaseContentNegotiation: ... def get_exception_handler(self) -> Callable: ... diff --git a/tests/typecheck/test_views.yml b/tests/typecheck/test_views.yml index 57320f2a8..00c70be5b 100644 --- a/tests/typecheck/test_views.yml +++ b/tests/typecheck/test_views.yml @@ -69,11 +69,22 @@ main: | from typing import List + from rest_framework.viewsets import GenericViewSet + from rest_framework.permissions import BasePermission, IsAdminUser + + class MyView(GenericViewSet): + def get_permissions(self) -> List[BasePermission]: + ... + +- case: test_override_get_permissions_super + main: | + from typing import Sequence + from rest_framework.viewsets import GenericViewSet from rest_framework.permissions import _SupportsHasPermission class MyView(GenericViewSet): - def get_permissions(self) -> List[_SupportsHasPermission]: + def get_permissions(self) -> Sequence[_SupportsHasPermission]: return super().get_permissions() - case: test_override_get_permissions_operandholder @@ -81,8 +92,8 @@ from typing import Sequence from rest_framework.viewsets import GenericViewSet - from rest_framework.permissions import BasePermission, IsAuthenticated, IsAdminUser, _SupportsHasPermission + from rest_framework.permissions import AND, IsAuthenticated, IsAdminUser, _SupportsHasPermission class MyView(GenericViewSet): def get_permissions(self) -> list[_SupportsHasPermission]: - return [IsAuthenticated & IsAdminUser] + return [AND(IsAuthenticated(), IsAdminUser())]