djangorestframework-stubs icon indicating copy to clipboard operation
djangorestframework-stubs copied to clipboard

Argument 1 of "perform_create" is incompatible with supertype "CreateModelMixin"; supertype defines the argument type as "BaseSerializer"

Open sobolevn opened this issue 6 years ago • 5 comments

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.

sobolevn avatar Aug 25 '19 21:08 sobolevn

need to be retested - i think this should be resolved now @sobolevn

Goldziher avatar Oct 10 '20 11:10 Goldziher

@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.

sobolevn avatar Oct 11 '20 09:10 sobolevn

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 avatar Mar 10 '21 16:03 w0rp

@w0rp PR is more than welcome! 👍

sobolevn avatar Mar 10 '21 16:03 sobolevn

I'll set one up now through GitHub, and you can let me know what else needs to be changed.

w0rp avatar Mar 10 '21 16:03 w0rp