djangorestframework-stubs
djangorestframework-stubs copied to clipboard
Argument 1 of "perform_create" is incompatible with supertype "CreateModelMixin"; supertype defines the argument type as "BaseSerializer"
Code:
from rest_framework import viewsets
from server.apps.main.logic import repo
from server.apps.main.serializers import BlogPostSerializer
from server.apps.main.models import BlogPost
class BlogPostViewset(viewsets.ModelViewSet):
"""API demo."""
serializer_class = BlogPostSerializer
queryset = repo.published_posts()
def perform_create(self, serializer: BlogPostSerializer) -> None:
serializer.save()
Output:
» PYTHONPATH="$PYTHONPATH:$PWD" mypy server
server/apps/main/views.py:28: error: Argument 1 of "perform_create" is incompatible with supertype "CreateModelMixin"; supertype defines the argument type as "BaseSerializer"
Reproduction: https://github.com/sobolevn/django_stubs_example
I guess that variance of the argument is incorrect.
need to be retested - i think this should be resolved now @sobolevn
@Goldziher let's add a test for it as well! Because I still think that this is revelant: https://github.com/typeddjango/djangorestframework-stubs/blob/master/rest_framework-stubs/mixins.pyi#L10
It still has BaseSerializer there.
I saw this same issue, and I believe the TypeVar for BaseSerializer needs to be created as covariant with covariant=True. The TypeVar for UsesQuerySet is set up that way, so I think it was just something overlooked before.
I tried using serializer: BaseSerializer[MyModelType] for the method override to try and fix the error, and I got another type error saying the generic type is invariant.
@w0rp PR is more than welcome! 👍
I'll set one up now through GitHub, and you can let me know what else needs to be changed.