Replies: 1 comment 1 reply
|
There's no built-in "annotated is a subtype of unannotated" relation — the annotation is tracked in the generic parameter and treated invariantly, so from typing import cast
def make_my_filtered_queryset(qs: MyQuerySet) -> MyQuerySet:
annotated = qs.annotate(some_count=Count(...)).filter(some_count=1)
return cast("MyQuerySet[MyModel]", annotated)That keeps the annotation visible inside the function (so |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
So in making my QuerySets defined as generic so that django-stubs starts to understand .annotate properly I have a problem where I don't want to always recognise that a queryset is annotated.
For example
After I make my queryset generic it starts to complain at me along the lines of
I'd rather not add several hundred TypedDicts for annotations that aren't relevant out of the immediate .filter that uses them.
I'm wondering what y'all think about this situation and whether it's reasonable for mypy to say an annotated queryset is a superset of it's unannotated equivalent?
All reactions