You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Remove QuerySet alias hacks via PEP 696 TypeVar defaults (#2104)
The `QuerySet` class was previously named `_QuerySet` and had three aliases: `QuerySet`, `QuerySetAny` and `ValuesQuerySet`.
These hacks were mainly needed to for the ergonomic single-parameter `QuerySet[Model]`, which expanded into `_QuerySet[Model, Model]`
But now that mypy 1.10 implements PEP 696 to a fuller extent (Pyright also supports it), the 2nd type parameter can be a simple TypeVar that defaults to 1st type parameter.
### How do I check if something is an instance of QuerySet in runtime?
258
-
259
-
A limitation of making `QuerySet` generic is that you can not use
260
-
it for `isinstance` checks.
261
-
262
-
```python
263
-
from django.db.models.query import QuerySet
264
-
265
-
deffoo(obj: object) -> None:
266
-
ifisinstance(obj, QuerySet): # Error: Parameterized generics cannot be used with class or instance checks
267
-
...
268
-
```
269
-
270
-
To get around with this issue without making `QuerySet` non-generic,
271
-
Django-stubs provides `django_stubs_ext.QuerySetAny`, a non-generic
272
-
variant of `QuerySet` suitable for runtime type checking:
273
-
274
-
```python
275
-
from django_stubs_ext import QuerySetAny
276
-
277
-
deffoo(obj: object) -> None:
278
-
ifisinstance(obj, QuerySetAny): # OK
279
-
...
280
-
```
281
-
282
257
### Why am I getting incompatible argument type mentioning `_StrPromise`?
283
258
284
259
The lazy translation functions of Django (such as `gettext_lazy`) return a `Promise` instead of `str`. These two types [cannot be used interchangeably](https://github.com/typeddjango/django-stubs/pull/1139#issuecomment-1232167698). The return type of these functions was therefore [changed](https://github.com/typeddjango/django-stubs/pull/689) to reflect that.
reveal_type(self.method_action_bare) # N: Revealed type is "def (django.http.request.HttpRequest, django.db.models.query._QuerySet[main.MyModel, main.MyModel])"
112
-
reveal_type(self.method_action_fancy) # N: Revealed type is "def (django.http.request.HttpRequest, django.db.models.query._QuerySet[main.MyModel, main.MyModel])"
113
-
reveal_type(self.method_action_http_response) # N: Revealed type is "def (django.http.request.HttpRequest, django.db.models.query._QuerySet[main.MyModel, main.MyModel]) -> django.http.response.HttpResponse"
114
-
reveal_type(self.method_action_file_response) # N: Revealed type is "def (django.http.request.HttpRequest, django.db.models.query._QuerySet[main.MyModel, main.MyModel]) -> django.http.response.FileResponse"
111
+
reveal_type(self.method_action_bare) # N: Revealed type is "def (django.http.request.HttpRequest, django.db.models.query.QuerySet[main.MyModel, main.MyModel])"
112
+
reveal_type(self.method_action_fancy) # N: Revealed type is "def (django.http.request.HttpRequest, django.db.models.query.QuerySet[main.MyModel, main.MyModel])"
113
+
reveal_type(self.method_action_http_response) # N: Revealed type is "def (django.http.request.HttpRequest, django.db.models.query.QuerySet[main.MyModel, main.MyModel]) -> django.http.response.HttpResponse"
114
+
reveal_type(self.method_action_file_response) # N: Revealed type is "def (django.http.request.HttpRequest, django.db.models.query.QuerySet[main.MyModel, main.MyModel]) -> django.http.response.FileResponse"
0 commit comments