Skip to content
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

Typing issue with actions of generic ModelViewSet #728

Open
H4rryK4ne opened this issue Feb 5, 2025 · 0 comments
Open

Typing issue with actions of generic ModelViewSet #728

H4rryK4ne opened this issue Feb 5, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@H4rryK4ne
Copy link

Bug report

What's wrong

I have defined a BaseModelViewSet

from abc import ABCMeta, abstractmethod
from http import HTTPMethod

from django.db.models import QuerySet
from rest_framework.decorators import action
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.serializers import ModelSerializer
from rest_framework.viewsets import ModelViewSet

from my_app.models import SomeBaseModel


class BaseModelViewSet[_M: SomeBaseModel](ModelViewSet[_M], metaclass=ABCMeta):
    queryset: QuerySet[_M]
    serializer_class: type[ModelSerializer[_M]]

    @abstractmethod
    @action(detail=False, methods=[HTTPMethod.GET])
    def some_action(self, request: Request) -> Response:
        raise NotImplementedError

And a child view set

from my_app.models import ConcreteModel
from my_app.serializers import ConcreteModelSerializer

class ConcreteModelViewSet(BaseModelViewSet[ConcreteModel]):
    queryset = ConcreteModel.objects.all()
    serializer_class = ConcreteModelSerializer

    @override
    @action(detail=False, methods=[HTTPMethod.GET])
    def some_action(self, request: Request) -> Response:
        return Response({})

I get the error

my_app/view_rest.py:316: error: Signature of "some_action" incompatible with supertype "BaseModelViewSet"  [override]
        def some_action(self, request: Request) -> Response:
        ^
my_app/view_rest.py:316: note:      Superclass:
my_app/view_rest.py:316: note:          ViewSetAction[Callable[[BaseModelViewSet[_M], Request], Response]]
my_app/view_rest.py:316: note:      Subclass:
my_app/view_rest.py:316: note:          ViewSetAction[Callable[[ConcreteModelViewSet, Request], Response]]

If I remove the @action decorator mypy passes.

How is that should be

Mypy should pass

System information

  • OS:
  • python version: 3.12.7
  • django version: 5.1.1
  • mypy version: 1.14.1
  • django-stubs version: 5.1.2
@H4rryK4ne H4rryK4ne added the bug Something isn't working label Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

1 participant