django-stubs
django-stubs copied to clipboard
Incomplete `Exists` argument annotation
Bug report
What's wrong
The Exists class' first argument is currently type hinted as queryset: Union[Query, QuerySet]. I think Subquery or a TypeVar bound by Subquery should be added to the union.
This code currently fails type checking:
from django.db.models import Exists, Subquery
from app.models import Project
qs = Project.objects.filter(
Exists(
Subquery(
Project.objects.filter(status='created')
)
)
)
test.py:6:12: error: Argument 1 to "Exists" has incompatible type "Subquery"; expected "Union[Query, _QuerySet[Any, Any]]" [arg-type]
Found 1 error in 1 file (checked 1 source file)
but in reality, both of these snippets work and produce identical queries
qs1 = Project.objects.filter(Exists(Subquery(Project.objects.filter(status='created'))))
qs2 = Project.objects.filter(Exists(Project.objects.filter(status='created')))
print(qs1.query == qs2.query) # True
I'd be happy to open a PR if this is accepted 👍