Skip to content

TypeError: type 'GenericViewSet' is not subscriptable #734

Open
@Alfro

Description

@Alfro

Bug report

What's wrong

On the following code:

from rest_framework import mixins, viewsets

class MyModelListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet): ...

I am getting the following error:

error: Missing type parameters for generic type "GenericViewSet" [type-arg]

because I'm not adding a [Type] to GenericViewSet. However, if I do it like so:

from rest_framework import mixins, viewsets

class MyModelListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet[MyModel]): ...

I will get a "not subscriptable" error on execution like so:

TypeError: type 'GenericViewSet' is not subscriptable

How is that should be

The DRF GenericViewSet and its type definition should be compatible. If the viewset is not subscriptable, the stubs should not require a type.

System information

  • OS:
  • python version: 3.11.11
  • django version: 5.1.6
  • djangorestframework version: 3.14.0
  • djangorestframework-stubs version: 3.14.5
  • mypy version: 1.11.1
  • django-stubs version: 4.2.7

Is this a version incompatibility issue? I've tried to see if newer versions of djangorestframework have a subscriptable ViewSet.

Please do let me know what I'm doing wrong and feel free to retag this if needed. Since the GenericViewSet definition has been like this for a long time, I have a feeling I must just be misunderstanding something.

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