-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
isinstance(..., collections.abc.Callable) complains about "_SpecialForm" #6864
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
Why it was not possible? Generally I think we should try to limit "leaking" the internal things like |
there were a few cases where the abc types were used as a first-class object Here's a contrived minimal example that's similar and also has the same failure mode: from typing import Tuple, Type
from collections.abc import Callable
from collections.abc import Sized
classifier: Tuple[Tuple[str, Type], ...] = (
('callable', Callable),
('sized', Sized),
) $ ./venv/bin/mypy t.py
t.py:5: error: Incompatible types in assignment (expression has type "Tuple[Tuple[str, _SpecialForm], Tuple[str, Type[Sized]]]", variable has type "Tuple[Tuple[str, Type[Any]], ...]") |
I'm going to close this as a duplicate of #6680 (it's the same bug), and add a comment to that issue. |
I think that's slightly different -- that one is about |
They look pretty similar to me at runtime: Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections.abc import Callable
>>> isinstance(lambda x: x, Callable[[object], object])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: isinstance() argument 2 cannot be a parameterized generic
>>> isinstance(lambda x: x, Callable)
True
>>> from typing import Tuple
>>> isinstance((1, 2), Tuple[int, int])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\alexw\AppData\Local\Programs\Python\Python310\lib\typing.py", line 994, in __instancecheck__
return self.__subclasscheck__(type(obj))
File "C:\Users\alexw\AppData\Local\Programs\Python\Python310\lib\typing.py", line 997, in __subclasscheck__
raise TypeError("Subscripted generics cannot be used with"
TypeError: Subscripted generics cannot be used with class and instance checks
>>> isinstance((1, 2), Tuple)
True
>>> And from mypy's perspective, they're identical, since typeshed just does |
this issue predates PEP 585 and doesn't involve generics |
I'm aware of that. But if you read my example in full, I show that For both |
yeah that's precisely my point -- this issue doesn't involve subscripted isinstance at all whereas the other issue is focused on that but you're the triager I'm just a rando so feel free to ignore me 👍 |
The other issue is actually now only being kept open because of the false-positive when an unsubscripted |
input
output
fix (for me)
the fix for me is to just use
callable(...)
, however in some cases this wasn't possible and so for now I'm# type: ignore
ingThe text was updated successfully, but these errors were encountered: