I'm reading PEP 544, specifically the section for class objects vs protocols, and I cannot understand the last example given there, I'll copy paste it here:
A class object is considered an implementation of a protocol if accessing all members on it results in types compatible with the protocol members. For example:
from typing import Any, Protocol
class ProtoA(Protocol):
def meth(self, x: int) -> int: ...
class ProtoB(Protocol):
def meth(self, obj: Any, x: int) -> int: ...
class C:
def meth(self, x: int) -> int: ...
a: ProtoA = C # Type check error, signatures don't match!
b: ProtoB = C # OK
I can get the rest of the PEP, but this example seems counterintuitive to me. The way I would think it is that the class C implements the method meth with the same signature as ProtoA, so why the heck is an error in line a: ProtoA = C?
And why b: ProtoB = C is correct? The signature of C.meth is different than the signature of ProtoB.meth (the latter includes an extra argument obj: Any.
Can someone explain this by expanding the concept so I can understand it?