question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GenericAPIView has missing type parameters, but when added 'type' object is not subscriptable

See original GitHub issue

Bug report

What’s wrong

When using mypy with strict=true, it issues the following error: Missing type parameters for generic type "GenericAPIView".

So, I check the type and add it, but I’m now getting a runtime error: TypeError: 'type' object is not subscriptable.

I am using django_stubs_ext.monkeypatch(), which solves the problem for other similar issues.

I haven’t test it on its own, but this should suffice, as a reproducible test:

from rest_framework.generics import GenericAPIView

class ShouldWorkView(GenericAPIView[None]):
   ...

How is that should be

The above example should pass, with no issues.

System information

  • OS: Arch
  • python version: 3.10
  • django version: 4.0.7
  • mypy version: 0.942
  • django-stubs version: 1.12.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
monosanscommented, Nov 18, 2022

You can pass the classes you want to monkeypatch in the extra_classes parameter to django_stubs_ext.monkeypatch().

Example:

import django_stubs_ext
from rest_framework import fields, generics

django_stubs_ext.monkeypatch(
    extra_classes=(fields.Field, generics.GenericAPIView)
)

or you can manually monkeypatch them:

from rest_framework import fields, generics

for cls in (fields.Field, generics.GenericAPIView):
    cls.__class_getitem__ = classmethod(  # type: ignore[attr-defined]
        lambda cls, *args, **kwargs: cls
    )
1reaction
ewinercommented, Sep 22, 2022

I believe you’re hitting this issue: https://mypy.readthedocs.io/en/latest/runtime_troubles.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime. The solution suggested on that doc page worked for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Missing type parameters for generic type "File" · Issue #1061 ...
error: Missing type parameters for generic type "File" #1061 ... TypeError: 'type' object is not subscriptable. The text was updated ...
Read more >
Why isn't this function type-annotated correctly (error: Missing ...
Supply the annotation as a string. The Python interpreter (runtime) will not interpret the annotation, but mypy picks up the proper meaning.
Read more >
Python TypeError: 'type' object is not subscriptable Solution
On Career Karma, learn the cause of and the solution to the common TypeError: 'type' object is not subscriptable Python error.
Read more >
Machinalis: Blog
Having to add types to arguments helped me notice several unused arguments to functions that could just be removed, simplifying code for free....
Read more >
python/typing - Gitter
Hi, i've made stubs for some package, and while making had not problems with types resolving, but at finish, met several problems with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found