-
-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QuerySetAlias as a non-generic variant of QuerySet.
This also re-export `QuerySetAlias` for external access to nongeneric QuerySet. The approach taken here is making `_QuerySetAlias` a subclass of `_QuerySet[_T, _T]` dedicated for isinstance checks, and leave `QuerySet` unchanged as the type alias of `_QuerySet[_T, _T]`. A slight disadvantage of this approach is that `QuerySet` is not a subclass of `QuerySetAlias`, and thus using `QuerySetAlias` in type annotation is generally incorrect. But the fix should suffice for the `isinstance` check use case. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
- Loading branch information
Showing
5 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import typing | ||
|
||
if typing.TYPE_CHECKING: | ||
from django.db.models.query import _T, _QuerySet, _Row | ||
from django.db.models.query import _T, _QuerySet, _QuerySetAlias, _Row | ||
from django.utils.functional import _StrOrPromise as StrOrPromise | ||
from django.utils.functional import _StrPromise as StrPromise | ||
|
||
QuerySetAlias = _QuerySetAlias | ||
ValuesQuerySet = _QuerySet[_T, _Row] | ||
else: | ||
from django.db.models.query import QuerySet | ||
from django.utils.functional import Promise as StrPromise | ||
|
||
QuerySetAlias = QuerySet | ||
ValuesQuerySet = QuerySet | ||
StrOrPromise = typing.Union[str, StrPromise] | ||
|
||
__all__ = ["StrOrPromise", "StrPromise", "ValuesQuerySet"] | ||
__all__ = ["StrOrPromise", "StrPromise", "QuerySetAlias", "ValuesQuerySet"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
- case: queryset_isinstance_check | ||
main: | | ||
from typing import Any | ||
from django.db.models.query import QuerySet | ||
from django_stubs_ext import QuerySetAlias | ||
def foo(q: QuerySet[Any]) -> None: | ||
pass | ||
def bar(q: QuerySetAlias) -> None: | ||
pass | ||
def baz(obj: object) -> None: | ||
if isinstance(obj, QuerySetAlias): | ||
reveal_type(obj) # N: Revealed type is "django.db.models.query._QuerySetAlias[Any]" | ||
foo(obj) | ||
bar(obj) | ||
if isinstance(obj, QuerySet): # E: Parameterized generics cannot be used with class or instance checks | ||
reveal_type(obj) # N: Revealed type is "django.db.models.query._QuerySet[Any, Any]" | ||
foo(obj) | ||
bar(obj) # E: Argument 1 to "bar" has incompatible type "_QuerySet[Any, Any]"; expected "_QuerySetAlias[Any]" |