Skip to content

TypeError when using QuerySet generic #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nuschk opened this issue Apr 23, 2019 · 2 comments
Closed

TypeError when using QuerySet generic #69

nuschk opened this issue Apr 23, 2019 · 2 comments

Comments

@nuschk
Copy link

nuschk commented Apr 23, 2019

This is probably a silly question. I'm getting runtime errors when using the QuerySet[] generic.

Consider this file:

mypy_test.py

from django.contrib.auth.models import User, Permission
from django.db.models.query import QuerySet


def f(queryset: QuerySet[User]) -> QuerySet[User]:
    return queryset.order_by('id')


def g() -> None:
    print(f(User.objects.all()))
    print(f(Permission.objects.all()))

Mypy is working as expected (which tells me, I've configured django-stubs correctly):

mypy src/mypy_test.py
src/mypy_test.py:11: error: Argument 1 to "f" has incompatible type "QuerySet[Permission, Permission]"; expected "QuerySet[User, User]"
exit 1

Only, I can't import this file:

In [1]: import mypy_test
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-040518e7b0e9> in <module>
----> 1 import mypy_test

~/Projects/flatfox-website/src/mypy_test.py in <module>
      3
      4
----> 5 def f(queryset: QuerySet[User]) -> QuerySet[User]:
      6     return queryset.order_by('id')
      7

TypeError: 'type' object is not subscriptable

In [2]:

Why is the interpreter even trying to make sense of the type annotations? I'm confused, appreciating any help!

Oh, and many thanks for your work on this package!

mypy.ini

# Global options:

[mypy]
python_version = 3.6
warn_return_any = True
warn_unused_configs = True
ignore_missing_imports = True
plugins =
    mypy_django_plugin.main

I don't have a mypy_django.ini.

@mkurnikov
Copy link
Member

Use quotes around the annotation like that, 'QuerySet[Model]', as QuerySet does not yet support __class_getitem__ and therefore not subscriptable.

@nuschk
Copy link
Author

nuschk commented Apr 23, 2019

Ugh, that would never have occurred to me. Thanks!

@nuschk nuschk closed this as completed Apr 23, 2019
@TonyRippy TonyRippy mentioned this issue Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants