-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
isinstance(x, Tuple) fails when equivalent expressions don't #6680
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
I am guessing the sentence you quote from the docs applies to its use in type annotations, not in |
Ah, I see that now. If you don't mind me asking, why does
don't raise TypeErrors unless I parameterize the generics. |
Because |
That makes sense. Thanks for the guidance! |
Essentially, the mypy behavior is opposite to what happens at runtime: isinstance((), Tuple) # Works at runtime, error in mypy
isinstance((), Tuple[Any, ...]) # Error at runtime, no error in mypy I think however this is a low priority, you should really just use |
On 0.941, mypy now correctly emits an error for from typing import Any, Tuple
x = (1,2)
isinstance(x, tuple)
isinstance(x, Tuple[Any, ...]) # error: Argument 2 to "isinstance" has incompatible type "object"; expected "Union[type, UnionType, Tuple[Union[type, UnionType, Tuple[Any, ...]], ...]]"
isinstance(x, Tuple) # error: Argument 2 to "isinstance" has incompatible type "_SpecialForm"; expected "Union[type, UnionType, Tuple[Union[type, UnionType, Tuple[Any, ...]], ...]]" |
(Closed by accident) |
So what's to do here? The OP is about runtime behavior, which should not be discussed in the mypy tracker. |
I read the OP as being about the false-positive error mypy emits on this snippet of code: from typing import Tuple
isinstance(x, Tuple) This works fine at runtime, but mypy 0.942 reports:
It's hardly a priority to fix, but it does seem to me to be a legitimate bug? |
#14014 is likely also a duplicate. This issue persists when checking against match x:
case collections.abc.Callable() as func:
... Literally all other classes in |
reproducer
observed behavior
expected behavior
The python documentation states
If the above is true, IMO either all of the examples should raise errors, or none of them should. Only the third example is raising an error right now. I'm not quite sure what the error means in any case.
environment
master as of 2019-04-15
As always, thanks for all the work you do here!
The text was updated successfully, but these errors were encountered: