You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect example of Protocol type check in documentation #2176
On the page "Protocols — typing documentation" in section "type[] and class objects vs protocols" is given wrong example: instead of class instances are given class references. Even more, error and success cases are mismatched:
changed the title [-]Bad example of Protocol implementation check in documentation[/-][+]Incorrect example of Protocol type check in documentation[/+]on Feb 13, 2026
Arguably this example is incorrect as written, but for the subtler reason that the obj parameter in ProtoB.meth should be positional-only, otherwise its name should matter for assignability.
But are you really saying that this is correct behavior?:
a: ProtoA = C # Type check error, signatures don't match!
Yes. C.meth has the signature (self: C, x: int) -> int. ProtoA.meth has the signature (x: int) -> int. The former signature is not assignable to the latter.
The protocol is interpreted as an instance type, so the method is bound; the self argument "disappears". But C.meth accessed off the class is an unbound method, so it still has its self argument. The idea of the example is that that self ends up mapping to the obj: Any argument in ProtoB.meth.
Activity
[-]Bad example of Protocol implementation check in documentation[/-][+]Incorrect example of Protocol type check in documentation[/+]e1turin commentedon Feb 13, 2026
I suggest such a change: #2177
JelleZijlstra commentedon Feb 14, 2026
No, this is correct. Class objects can match protocols.
carljm commentedon Feb 14, 2026
Arguably this example is incorrect as written, but for the subtler reason that the
objparameter inProtoB.methshould be positional-only, otherwise its name should matter for assignability.e1turin commentedon Feb 14, 2026
Well, now I understood what was going on. But are you really saying that this is correct behavior?:
carljm commentedon Feb 14, 2026
Yes.
C.methhas the signature(self: C, x: int) -> int.ProtoA.methhas the signature(x: int) -> int. The former signature is not assignable to the latter.The protocol is interpreted as an instance type, so the method is bound; the
selfargument "disappears". ButC.methaccessed off the class is an unbound method, so it still has itsselfargument. The idea of the example is that thatselfends up mapping to theobj: Anyargument inProtoB.meth.e1turin commentedon Feb 14, 2026
Oh, I see now. Thank you!