The following code defines a simple protocol and a class which almost implements that protocol. The only difference is the fact that the run() method takes an argument in the protocol, but it's not implemented that way.
However, the isinstance() check returns true which was unexpected.
As I understood PEP-544 this should work. Although it's pretty unclear about checking function signatures.
The same issue happens when using the wrong data-type in the ORDER member (f.ex. changing it to str in the protocol).
I'm aware that type-hints are just... well... "hints" and are not enforced at runtime.
However, in my application it would be useful to ensure certain classes follow a defined protocol for clearer error-messages. It is using a plugin-architecture, and after loading a plugin it would be useful to have a quick "sanity-check" if that plugin follows the required protocol and if not, give an early and useful error-message instead of causing an exception later on downstream.
from typing_extensions import Protocol, runtime_checkable
@runtime_checkable
class PFoo(Protocol):
ORDER: int
def run(self, a: int) -> None:
...
class Hello:
ORDER = 10
def run(self) -> None:
print(1)
# Returns "True" evn though the signature of "run()" doesn't match
print(isinstance(Hello(), PFoo))