django-stubs icon indicating copy to clipboard operation
django-stubs copied to clipboard

Incomplete `Exists` argument annotation

Open sondrelg opened this issue 3 years ago • 1 comments

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

sondrelg avatar Jun 07 '22 22:06 sondrelg

I'd be happy to open a PR if this is accepted 👍

sondrelg avatar Jun 07 '22 22:06 sondrelg