-
-
Notifications
You must be signed in to change notification settings - Fork 450
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
Question regarding typing a values_list with (flat=True) #1047
Comments
You can use I am going to close this as non issue. Please, feel free to reopen if something in my answer does not work for you. |
Thank you for the response. Setting Sequence or List as a return type doesn't resolve the issue. I've create a new more contained example: models.py:
Which has the following error: |
Hm, looks like our |
I'm having a similar issue so I did some digging: The custom queryset representation is currently: class _QuerySet(Generic[_T, _Row], Collection[_Row], Reversible[_Row], Sized): To allow your syntax, we would need: class _QuerySet(Generic[_T, _Row], Sequence[_Row]): From the docs, a Sequence inherit from I see a few options to solve your case:
.values_list("category", flat=True) -> ValuesQuerySet[Exam, str]
.values_list("category") -> ValuesQuerySet[Exam, Tuple[str]]
.values("category") -> ValuesQuerySet[Exam, TypedDict(...)] PS: Took me some time to understand I could use that, and this issue confirm my feeling. Maybe a doc section about type annotation for
|
I experimented a bit, and quickly realised that the >>> type(Food.objects.all()) is type(Food.objects.values_list('spam', flat=True))
True
>>> set(dir(Food.objects.all())) == set(dir(Food.objects.values_list('spam', flat=True)))
True
>>> from django.db.models import QuerySet
>>> isinstance(Food.objects.values_list('spam', flat=True), QuerySet)
True Clearly, they are exactly the same type (i.e. So IMHO, the obvious way to type |
I'm thinking this was closed via suggestion Let me know if I this is an incorrect assumption. |
@flaeppe Unless I'm missing something, I believe that the |
Yes, it's still bound to @property
def category(self) -> QuerySet[Question, str]: ... |
Ok great, than I suppose it's indeed solved 🎉 |
Bug report
What's wrong
I can't set the correct type on the return value of a values list.
Simplified example:
How is that should be
The calling code needs to loop over the categories, i.e. it needs a sequence of type str.
What is the best way to annotate the get_result function?
System information
python
version: 3.9.7django
version: 3.2.13mypy
version: 0.961django-stubs
version: 1.12.0django-stubs-ext
version: 0.4.0The text was updated successfully, but these errors were encountered: