Skip to content

Annotating GenericAPIView.filter_queryset fails since v3.16.9 #965

Description

@pai-martinreidemeister

Bug report

What's wrong

I don't know how to annotate rest_framework.generics.GenericAPIView.filter_queryset. Before 3.16.9 we did this:

from typing import override
from django.db.models import QuerySet
from rest_framework import viewsets
from foo.models import MyModel

class MyModelViewSet(viewsets.ModelViewSet[MyModel]):
    queryset = MyModel.objects.all()
    filterset_fields = ('name',)

    @override
    def filter_queryset(self, queryset: QuerySet[MyModel]) -> QuerySet[MyModel]:
        qs = super().filter_queryset(queryset)
        return qs

But since 3.16.9 we get this error:

$ mypy .
/path/to/foo/views_rest.py:12: error: Return type "QuerySet[MyModel, MyModel]" of "filter_queryset" incompatible with return type "QuerySet[MyModel, _Row]" in supertype
"rest_framework.generics.GenericAPIView"  [override]
        def filter_queryset(self, queryset: QuerySet[MyModel]) -> QuerySet[MyModel]:
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/path/to/foo/views_rest.py:12: error: Argument 1 of "filter_queryset" is incompatible with supertype "rest_framework.generics.GenericAPIView"; supertype defines the argument type as
"QuerySet[MyModel, _Row]"  [override]
        def filter_queryset(self, queryset: QuerySet[MyModel]) -> QuerySet[MyModel]:
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
/path/to/foo/views_rest.py:12: note: This violates the Liskov substitution principle
/path/to/foo/views_rest.py:12: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
Found 2 errors in 1 file (checked 13 source files)

I assume that #916 is the cause.

How is that should be

There should be no error.

System information

  • OS:
  • python version: 3.13.12
  • django version: 6.0.3
  • mypy version: 1.20.1
  • django-stubs version: 6.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions