-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
__instancecheck__
on an instance works at runtime, but mypy thinks it's invalid
#12974
Comments
Here's the current typeshed stub for import types
from typing import Any
def isinstance(
__obj: object, __class_or_tuple: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, ...], ...]
) -> bool: ... We could change this to something like this: import types
from typing import Any, Protocol
class _SupportsInstanceCheck(Protocol):
def __instancecheck__(self, other: object) -> bool: ...
def isinstance(
__obj: object, __class_or_tuple: type | types.UnionType | _SupportsInstanceCheck | tuple[type | types.UnionType | _SupportsInstanceCheck | tuple[Any, ...], ...]
) -> bool: ... But I think doing so might be a bad idea. |
I think if this is to be supported, it does need to have a separate
Yeah, definitely, this is not a feature I'd push hard for. |
The point is that we'd have to make this change over at the typeshed repo rather than mypy (mypy vendors its stdlib stubs from typeshed). Mypy isn't the only type checker that uses typeshed's stubs: they're also used by pyright, pytype, pyre and pycharm. Are you prepared to file issues at all of them and make sure that this proposed change to the stub wouldn't break them? ;) |
Closing for the reasons I mentioned above: I don't think this is worth the possible breakage to type checkers, sorry :) (This proposed change would also probably need to be discussed over at the typeshed repo rather than here, since the first thing that needs to happen in order for this to be fixed would be for the typeshed stubs to be changed.) |
Bug Report
Expected Behavior
This seems like it should be valid, since at runtime it prints
False
thenTrue
as one would expect.Actual Behavior
Your Environment
mypy.ini
(and other config files):** P.S. **
If you just want to close this one because this is not really what
isinstance
is supposed to mean in the context of static type checking, that kinda makes sense, I would not strongly argue that this really needs to work. It just seems somewhat at odds with the hookable runtime behavior ofisinstance
.The text was updated successfully, but these errors were encountered: