Skip to content
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] What is an alternative to isinstance(obj, QuerySet)? #704

Closed
martin-thoma opened this issue Sep 6, 2021 · 4 comments · Fixed by #1199
Closed

[Question] What is an alternative to isinstance(obj, QuerySet)? #704

martin-thoma opened this issue Sep 6, 2021 · 4 comments · Fixed by #1199

Comments

@martin-thoma
Copy link

I've recently got

Parameterized generics cannot be used with class or instance checks

with django-stubs==1.9.0 for isinstance(obj, QuerySet) when I run mypy over the code.

What is the recommended way to do it?

@sobolevn
Copy link
Member

sobolevn commented Sep 6, 2021

This is because QuerySet is now = _QuerySet[_T, _T] from cfd69c0#diff-389fe7d7dcfb77ddaac87c1f3bdc196f3ef0ca33b8047c6638a15f3a3db21b56R170

We can try to alias it as QuerySet = _QuerySet it should be the same thing, but without subscription.

@MT-Cash can you please try that?

@martin-thoma
Copy link
Author

From where should I import _QuerySet?

@sobolevn
Copy link
Member

sobolevn commented Sep 6, 2021

You cannot import _QuerySet, because it is our only-in-stubs thing. You can send a Pr with the [_T, _T] removed from this line: cfd69c0#diff-389fe7d7dcfb77ddaac87c1f3bdc196f3ef0ca33b8047c6638a15f3a3db21b56R170

It might help.

@jerch
Copy link

jerch commented Dec 28, 2021

Kinda stumbled over the same issue and can confirm, that removing [_T, _T] solved it for me.

Created PR #794.

PIG208 added a commit to PIG208/django-stubs that referenced this issue Oct 27, 2022
This also re-export `QuerySetAny` for external access to
nongeneric QuerySet.

The approach taken here is making `_QuerySetAny` an alias
of `_QuerySet[_T, _T]` dedicated for isinstance checks, and
leave `QuerySet` unchanged as the type alias of `_QuerySet[_T, _T]`.

Fixes typeddjango#704.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
PIG208 added a commit to PIG208/django-stubs that referenced this issue Oct 27, 2022
This also re-export `QuerySetAny` for external access to
nongeneric QuerySet.

The approach taken here is making `_QuerySetAny` an alias
of `_QuerySet[_T, _T]` dedicated for isinstance checks, and
leave `QuerySet` unchanged as the type alias of `_QuerySet[_T, _T]`.

Fixes typeddjango#704.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
PIG208 added a commit to PIG208/django-stubs that referenced this issue Oct 27, 2022
This also re-export `QuerySetAny` for external access to
nongeneric QuerySet.

The approach taken here is making `_QuerySetAny` an alias
of `_QuerySet[_T, _T]` dedicated for isinstance checks, and
leave `QuerySet` unchanged as the type alias of `_QuerySet[_T, _T]`.

Fixes typeddjango#704.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
sobolevn pushed a commit that referenced this issue Nov 3, 2022
This also re-export `QuerySetAny` for external access to
nongeneric QuerySet.

The approach taken here is making `_QuerySetAny` an alias
of `_QuerySet[_T, _T]` dedicated for isinstance checks, and
leave `QuerySet` unchanged as the type alias of `_QuerySet[_T, _T]`.

Fixes #704.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants