Skip to content

Add permissions.OperandHolder to decorators.permission_classes signature #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions rest_framework-stubs/decorators.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from typing import Any, Callable, List, Mapping, Optional, Sequence, Type, Union
from django.http.response import HttpResponseBase
from rest_framework.authentication import BaseAuthentication
from rest_framework.parsers import BaseParser
from rest_framework.permissions import BasePermission
from rest_framework.permissions import _PermissionClass
from rest_framework.renderers import BaseRenderer
from rest_framework.schemas.inspectors import ViewInspector
from rest_framework.throttling import BaseThrottle
Expand Down Expand Up @@ -75,9 +75,7 @@ def authentication_classes(
def throttle_classes(
throttle_classes: Sequence[Union[BaseThrottle, Type[BaseThrottle]]]
) -> Callable[[_View], _View]: ...
def permission_classes(
permission_classes: Sequence[Union[BasePermission, Type[BasePermission]]]
) -> Callable[[_View], _View]: ...
def permission_classes(permission_classes: Sequence[_PermissionClass]) -> Callable[[_View], _View]: ...
def schema(view_inspector: Optional[Union[ViewInspector, Type[ViewInspector]]]) -> Callable[[_View], _View]: ...
def action(
methods: Optional[_MIXED_CASE_HTTP_VERBS] = ...,
Expand Down
17 changes: 17 additions & 0 deletions tests/typecheck/test_decorators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,20 @@
from rest_framework.decorators import api_view
@api_view() # E: Value of type variable "_View" of function cannot be "Callable[[Any], List[Any]]"
def view_func2(request) -> list: ...

- case: permission_classes
main: |
from rest_framework.decorators import permission_classes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to test that & form works. You can also add tests for other ways if

  • they are missing
  • you have the time for it 🙂

reveal_type(permission_classes) # N: Revealed type is "def (permission_classes: typing.Sequence[Union[Type[rest_framework.permissions.BasePermission], rest_framework.permissions.OperandHolder, rest_framework.permissions.SingleOperandHolder]]) -> def [_View <: def (*Any, **Any) -> django.http.response.HttpResponseBase] (_View`-1) -> _View`-1"

- case: permission_classes_with_operators
main: |
from rest_framework.permissions import BasePermission, IsAuthenticated
from rest_framework.decorators import permission_classes

class Permission(BasePermission):
pass

permission_classes([IsAuthenticated & Permission])
permission_classes([IsAuthenticated | Permission])
permission_classes([~Permission])