-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
QuerySet's .annotate()
returns an incorrect type when chained with .values()
#602
Comments
@kracekumar I found some discussion around annotate on this PR. |
I would love to see it rebased and merged! 👍 |
@sidmitra saw the PR previously and since it was not updated, felt opening a bug report to understand overall picture. |
I rebased the PR :)
|
Thank you @syastrov and a good catch on Collection. Since the return result can be used for any filtering, annotated as |
annotate()
method.annotate()
not possible to chain with .values()
.annotate()
not possible to chain with .values()
.annotate()
returns an incorrect type when chained with .values()
For anyone interested, I just want to leave an update here that since #2319 the closest way of defining a return type is probably: def get_book_counts_for_author() -> QuerySet[WithAnnotations[Book, AuthorCount], AuthorCount]: That would still trigger a "Incompatible return type" error. But we need an improvement of |
Bug report
What's wrong
Iterating over the
annotate
result and accessing the aggregated item throwsModel
is not indexable.Mypy error
What should be the proper way to annotate the code here other than
type: ignore
?Failed Approaches
get_book_counts_for_author() -> QuerySet[AuthorCount]:
Output:
polls/models.py:69: error: Type argument "TypedDict('polls.models.AuthorCount', {'author': builtins.int})" of "QuerySet" must be a subtype of "django.db.models.base.Model"
get_book_counts_for_author() -> Union[QuerySet[Book], AuthorCount]:
Output:
__getitem__
,__iter__
likeBut output was
polls/models.py:65: error: Return type "Iterator[AuthorCount]" of "__iter__" incompatible with return type "Iterator[_T]" in supertype "QuerySet"
. The same problem happens with__getittem__
. AlsoQuerySet[AuthorType]
fails since it's not a instance ofModel
.Working approach
Is there a better way to annotate the code for the
annotate
return value. The bad part of the code is to keep checking the instance type in the consuming function and adding optional throughout the code.How is that should be
I don't know
System information
python
version:3.9.4
django
version:3.2
mypy
version:0.812
django-stubs
version:1.8.0
The text was updated successfully, but these errors were encountered: