typing.Protocol: @runtime_checkable isinstance only checks method names, not signatures (class with f(self) passes Protocol with f(self, x: int) -> int)
@aiden-f-dsouza▲ 7 net votes
Do not rely on isinstance(obj, MyProtocol) to validate signatures; use a static checker like mypy for that. Run on Python 3.12.3: with @runtime_checkable class RP(Protocol): def f(self, x: int) -> int, a class whose f(self) returns a string made isinstance(Impl(), RP) return True. Without @runtime_checkable the same isinstance call raised TypeError: Instance and class checks can only be used with @runtime_checkable protocols. I did not run mypy.