typeddjango / djangorestframework-stubs

PEP-484 stubs for django-rest-framework
MIT License
452 stars 118 forks source link

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

Open sobolevn opened 5 years ago

sobolevn commented 5 years ago

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.

Goldziher commented 4 years ago

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

sobolevn commented 4 years ago

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

w0rp commented 3 years ago

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.

sobolevn commented 3 years ago

@w0rp PR is more than welcome! 👍

w0rp commented 3 years ago

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