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

Mypy complains about missing type parameters for ModelViewSet #371

Closed
OvervCW opened this issue Feb 28, 2023 · 1 comment
Closed

Mypy complains about missing type parameters for ModelViewSet #371

OvervCW opened this issue Feb 28, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@OvervCW
Copy link

OvervCW commented Feb 28, 2023

Bug report

What's wrong

I'm following along with the quickstart guide for the Django REST framework and I have the following snippet of code:

from django.contrib.auth.models import Group, User
from rest_framework import permissions, viewsets

from xxx.schemas.serializers import GroupSerializer, UserSerializer


class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """

    queryset = User.objects.all().order_by("-date_joined")
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated]

When I run mypy --strict . it complains:

src/xxx/schemas/views.py:7: error: Missing type parameters for generic type "ModelViewSet"  [type-arg]

If I change the line to

class UserViewSet(viewsets.ModelViewSet[User]):

then mypy is happy, but I am no longer able to run my application:

TypeError: type 'ModelViewSet' is not subscriptable

How is that should be

How do I write my code such that both mypy and Python are happy?

System information

  • OS:
  • python version: Ubuntu 22.0.4.2 LTS
  • django version: 4.1.7
  • mypy version: 1.0.1
  • django-stubs version: 1.15.0
  • djangorestframework-stubs version: 1.9.1
@OvervCW OvervCW added the bug Something isn't working label Feb 28, 2023
@OvervCW
Copy link
Author

OvervCW commented Feb 28, 2023

Nevermind, duplicate of #255

I solved my problem by doing this in settings.py:

import django_stubs_ext
from rest_framework import viewsets

django_stubs_ext.monkeypatch(extra_classes=(viewsets.ModelViewSet,))

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