-
-
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
Consider changing _BaseQuerySet to QuerySet #551
Comments
This is a big one. Thanks a lot, @davidhalter, for reporting it! |
QuerySet at one point actually had 2 type parameters (I implemented it in https://github.com/typeddjango/django-stubs/pull/33/files#diff-389fe7d7dcfb77ddaac87c1f3bdc196f3ef0ca33b8047c6638a15f3a3db21b56R53). I believe it was removed due to concerns that the extra type parameter would scare users (e.g. it would be revealed in error messages), and it is not commonly used. As you pointed out, you'd have to replace There was some discussion of making it possible to have defaults for type parameters in Python: python/mypy#4236 but it didn't seem to amount to anything. Another idea would be to mark the class with decorator Edit: Possibly combined with this idea, we could make |
By the way, See also: #144 |
I think if you want to make helpers for users you should probably do something like this:
not 100% sure that works in Python. In Rust I would write this:
Or you could maybe also handle this case with a mypy plugin (not sure that's possible though). However I think the way it's currently typed is just wrong and incomplete. I would prefer the solution I just proposed, but I also understand that this is something pretty unfortunate, especially because |
@davidhalter I think
So maybe this issue is obsolete? Maybe |
@Feuermurmel Good point. I'm not sure about that, because the alias |
I agree, there are a few hacks involved, including Mypy 1.9 added initial support for PEP 696 (TypeVar defaults), which is a step towards fixing these. We should be able to get rid of these Related mypy issue: EDIT: Indeed, full PEP 696 suppot is being worked on. |
I think |
Bug report
What's wrong
A Jedi user realized that some things about Django stubs did not add up: davidhalter/jedi#1667.
I realized that the reason for this is that
_BaseQuerySet
does not exist in Django's code base. This is probably a "hack" to work with proper types so that mypy can deal with Django's dynamic nature.However this leads to frameworks like Jedi (and probably others) not properly infer types from Python to stubs and back.
How is that should be
I'm not sure what the best solution is for this issue. Maybe we can get rid of
_BaseQuerySet
entirely by using something likeclass ValuesQuerySet(QuerySet[T], Collection[_Row])
, but I'm not sure that's possible.Another way forward could be to move most of the methods like
filter
ontoQuerySet
and duplicate them onValuesQuerySet
.The best solution would probably be to make
QuerySet[T, Row]
. This would mean that QuerySet has two type generics. This is great, because this really maps to what a QuerySet actually is in Django. Of course this has the drawback that everywhere where we haveQuerySet[T]
now, we have to write it likeQuerySet[T, T]
. But then again this wouldn't affect much:System information
python
version: 3.6 - 3.10django
version: 3mypy
version: -django-stubs
version: 3d2534eThe text was updated successfully, but these errors were encountered: