-
Notifications
You must be signed in to change notification settings - Fork 259
isinstance + class indirectly deriving from SupportsInt results in "TypeError: Protocols cannot be used with isinstance()" #297
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
This is definitely a problem. A quick workaround would be to replace the import with from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import SupportsInt
else:
SupportsInt = object but that's pretty gross. I have a pretty straightforward fix, which still disallows isinstance() checks on classes that directly derive from _Protocol (such as SupportsAbs) but allows it for subclasses thereof (like your C1 and C2).
@ilevkivskyi What do you think of that fix? |
PS. The fix only works on top of #295. |
@gvanrossum As a quick fix this is perfectly OK. I would say that for a permanent fix we could think about making the rules the same as for ordinary generics. But before deciding on permanent solution, I would ask @JukkaL for his opinion (the permanent fix would probably depend on resolution of #11). |
Thanks @gvanrossum, I totally forgot about the |
Fixes #297 This allows to use isinstance() with classes that inherit from protocols in typing.py such as SupportsInt etc.
Uh oh!
There was an error while loading. Please reload this page.
The following assumes mypy 0.4.5 and CPython 3.5.2.
Let's have the code:
The code works:
mypy, however, will complain (as expected):
Let's try to make mypy happy:
Now mypy is happy but the code fails at runtime:
It seems that I'm put in a situation where I have to choose between static type safety and having the code actually work, am I missing something here? If not - what's the recommended solution to a problem like this?
The text was updated successfully, but these errors were encountered: