-
-
Notifications
You must be signed in to change notification settings - Fork 485
Get rid of ValuesQuerySet
#608
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
Comments
Related: 6522f30 |
But should a It might be relevant if I were to call If Django hadn't allowed users to "flaunt their individualism", this would be a lot simpler :) https://docs.djangoproject.com/en/3.2/ref/models/querysets/#django.db.models.query.QuerySet.values
Also, another weird edge-case is that you can call However, we can just decide to go after simplicity rather than absolute correctness, and accept that these uncommon patterns of use are likely to result in less type-checking or false positives. |
I thing that we can try to add the second type argument dynamically to Later, we can use |
That would be nice. Would you check the behavior in other type-checkers if you haven't provided all the type parameters? I'm not sure I see how using variadic generics will help? It seems like we have some fixed number of type parameters, but we should allow some defaults for them if not provided (except that is not currently possible). |
I am afraid that making 2 generic parameters to QuerySet and hacking it in the mypy plugin to default the second parameter to the first one will make it even less likely that django-stubs will work in other type-checkers than mypy. Are there any other options? I'll try to list some:
I think I vote for (3). What do you think @sobolevn?
It's a subclass, so I don't know why it wouldn't be allowed. Do you have any examples? Edit: Ah, I see, we have to change the current hierarchy by having
I think we should use 2 generic parameters to allow this, so if you didn't care which model it was about, you could use the annotation: |
I've given idea (3) a shot: #654 |
Yes, I agree that this seems like the best solution. |
I have already seen this discussed, but I cannot find the link. |
This currently has the drawback that error messages display the internal type _QuerySet, with both type arguments. See also discussion on typeddjango#661 and typeddjango#608. Fixes typeddjango#635: QuerySet methods on Managers (like .all()) now return QuerySets rather than Managers. Address code review by @sobolevn.
* QuerySet.annotate returns self-type. Attribute access falls back to Any. - QuerySets that have an annotated model do not report errors during .filter() when called with invalid fields. - QuerySets that have an annotated model return ordinary dict rather than TypedDict for .values() - QuerySets that have an annotated model return Any rather than typed Tuple for .values_list() * Fix .annotate so it reuses existing annotated types. Fixes error in typechecking Django testsuite. * Fix self-typecheck error * Fix flake8 * Fix case of .values/.values_list before .annotate. * Extra ignores for Django 2.2 tests (false positives due to tests assuming QuerySet.first() won't return None) Fix mypy self-check. * More tests + more precise typing in case annotate called before values_list. Cleanup tests. * Test and fix annotate in combination with values/values_list with no params. * Remove line that does nothing :) * Formatting fixes * Address code review * Fix quoting in tests after mypy changed things * Use Final * Use typing_extensions.Final * Fixes after ValuesQuerySet -> _ValuesQuerySet refactor. Still not passing tests yet. * Fix inheritance of _ValuesQuerySet and remove unneeded type ignores. This allows the test "annotate_values_or_values_list_before_or_after_annotate_broadens_type" to pass. * Make it possible to annotate user code with "annotated models", using PEP 583 Annotated type. * Add docs * Make QuerySet[_T] an external alias to _QuerySet[_T, _T]. This currently has the drawback that error messages display the internal type _QuerySet, with both type arguments. See also discussion on #661 and #608. Fixes #635: QuerySet methods on Managers (like .all()) now return QuerySets rather than Managers. Address code review by @sobolevn. * Support passing TypedDicts to WithAnnotations * Add an example of an error to README regarding WithAnnotations + TypedDict. * Fix runtime behavior of ValuesQuerySet alias (you can't extend Any, for example). Fix some edge case with from_queryset after QuerySet changed to be an alias to _QuerySet. Can't make a minimal test case as this only occurred on a large internal codebase. * Fix issue when using from_queryset in some cases when having an argument with a type annotation on the QuerySet. The mypy docstring on anal_type says not to call defer() after it.
We can consider this done! Thanks to @syastrov 🎉 |
Problems with
QuerySet
/ValuesQuerySet
separation:QuerySet
annotation, soValuesQuerySet
is not allowed there._BaseQuerySet
is not enough for small portion of casesT
type var hasmodels.Model
bound, which stops us from annotating thing with, for example,QuerySet[SomeTypedDict]
, which is valid when we use.values
andSomeTypedDict
represents the final result of this callThe text was updated successfully, but these errors were encountered: