Closed
Description
Currently, isinstance
with protocols uses the same logic as with nominal classes, i.e. only erases the type variables, but sometimes this may be not safe (as discussed in PEP 544), for example:
@runtime
class P(Protocol):
attr: int
class C:
attr = 'Hi!'
if isinstance(C(), P):
# this branch is considered unreachable by mypy, but will be executed
The idea is to perform a "deeper" erasure (setting all attribute types to Any
) when considering isinstance()
with protocols, thus mimicking the runtime behaviour.