-
-
Notifications
You must be signed in to change notification settings - Fork 504
Open
Labels
bugSomething isn't workingSomething isn't working
Description
Bug report
What's wrong
Even though a queryset is correctly typed with WithAnnotations
, this annotation is lost when accessing an instance in the queryset, which means we can't use this typing feature properly. Here is a minimal example:
class SomeQuerySet(QuerySet[Accommodation]):
pass
queryset = SomeQuerySet().annotate(hello=Value(True)).filter(is_on_shelf=True).annotate(hi=Value(False))
reveal_type(queryset) # CORRECT: Revealed type is "SomeQuerySet[WithAnnotations[Accommodation, TypedDict({'hello': Any, 'hi': Any})]]"
reveal_type(queryset.first()) # INCORRECT: Revealed type is "Union[Accommodation, None]"
reveal_type(queryset.get(pk=1)) # INCORRECT: Revealed type is "Accommodation"
reveal_type(queryset[0]) # INCORRECT: Revealed type is "Accommodation"
How should it be instead:
Revealed types should be:
reveal_type(queryset.first()) # Revealed type is "Union[WithAnnotations[Accommodation, TypedDict({'hello': Any, 'hi': Any})], None]"
reveal_type(queryset.get(pk=1)) # Revealed type is "WithAnnotations[Accommodation, TypedDict({'hello': Any, 'hi': Any})]"
reveal_type(queryset[0]) # Revealed type is "WithAnnotations[Accommodation, TypedDict({'hello': Any, 'hi': Any})]"
System information
- OS: Mac Os Monterey
python
version: 10.0.3django
version: 4.0.4mypy
version: 0.961django-stubs
version: currentmaster
(just after 1.12.0)django-stubs-ext
version: 0.5.0
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working