Skip to content
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

RawQuerySet has conflicting errors: "is not subscriptable" vs "Missing type parameter" #2236

Closed
hterik opened this issue Jun 25, 2024 · 2 comments · Fixed by #2316
Closed
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@hterik
Copy link

hterik commented Jun 25, 2024

Bug report

What's wrong

Using RawQuerySet as type annotation is not possible.
Without the generic parameter, mypy fails with is not subscriptable error.
With the generic parameter, python throws TypeError.

from django.db.models.query import RawQuerySet
from models import MyModel

# TypeError: type 'RawQuerySet' is not subscriptable
def foo() -> RawQuerySet[MyModel]:                  
    return MyModel.objects.raw("SELECT id FROM my_model")

# mypy error: Missing type parameters for generic type "RawQuerySet"  [type-arg]
def bar() -> RawQuerySet:                           
    return MyModel.objects.raw("SELECT id FROM my_model")

System information

  • OS: Ubuntu 24.04
  • python version: 3.12.3
  • django version: 5.0.6
  • mypy version: 1.10
  • django-stubs version: 5.0.2
  • django-stubs-ext version: 5.0.2
@hterik hterik added the bug Something isn't working label Jun 25, 2024
@hterik hterik changed the title 'RawQuerySet' has conflicting errors: "is not subscriptable" vs "Missing type parameter" RawQuerySet has conflicting errors: "is not subscriptable" vs "Missing type parameter" Jun 25, 2024
@flaeppe
Copy link
Member

flaeppe commented Jun 28, 2024

Looks like RawQuerySet is missing from the monkeypatch list of classes to add __class_getitem__ to:

_need_generic: List[MPGeneric[Any]] = [
MPGeneric(ModelAdmin),
MPGeneric(SingleObjectMixin),
MPGeneric(FormMixin),
MPGeneric(DeletionMixin),
MPGeneric(MultipleObjectMixin),
MPGeneric(BaseModelAdmin),
MPGeneric(Field),
MPGeneric(Paginator),
MPGeneric(BaseFormSet),
MPGeneric(BaseModelForm),
MPGeneric(BaseModelFormSet),
MPGeneric(ModelChoiceField),
MPGeneric(Feed),
MPGeneric(Sitemap),
MPGeneric(SuccessMessageMixin),
MPGeneric(FileProxyMixin),
MPGeneric(Lookup),
MPGeneric(BaseConnectionHandler),
MPGeneric(ExpressionWrapper),
MPGeneric(ReverseManyToOneDescriptor),
# These types do have native `__class_getitem__` method since django 3.1:
MPGeneric(QuerySet, (3, 1)),
MPGeneric(BaseManager, (3, 1)),
# These types do have native `__class_getitem__` method since django 4.1:
MPGeneric(ForeignKey, (4, 1)),
]

A PR is welcome for fixing that

@flaeppe flaeppe added the good first issue Good for newcomers label Jun 28, 2024
@intgr
Copy link
Collaborator

intgr commented Jun 29, 2024

As a work-around, you can also use quoted type hints, e.g.

def foo() -> "RawQuerySet[MyModel]":                  
    return MyModel.objects.raw("SELECT id FROM my_model")

or alternatively, add from __future__ import annotations to the beginning of the file, which causes evaluation of all type hints to be deferred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Development

Successfully merging a pull request may close this issue.

3 participants