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
I think we can say for sure that the break here is unreachable. This is the __subclasshook__ method that is monkey-patched onto all subclasses of typing.Protocol that do not define their own __subclasshook__ methods. This block of code in the __subclasshook__ method is inspecting the __annotations__ dictionary of other to see if it can find any protocol members in that dictionary. But we know that there can't be any protocol members in the __annotations__ dictionary, because if there were, that would make other a protocol with at least one non-callable member. If it's a protocol that has at least one non-callable member, the __subclasshook__ method is never called at all during isinstance() or issubclass() checks, because we raise TypeError in _ProtocolMeta.__subclasscheck__, short-circuiting the call to abc.ABCMeta.__subclasscheck__ that would call Protocol.__subclasshook__:
Bug report
If you apply this diff to
typing.py, all tests continue to pass:The
breakon line 1934 here is unreachable; thus, this whole block of code is pointless:cpython/Lib/typing.py
Lines 1929 to 1934 in 3af2dc7
I think we can say for sure that the
breakhere is unreachable. This is the__subclasshook__method that is monkey-patched onto all subclasses oftyping.Protocolthat do not define their own__subclasshook__methods. This block of code in the__subclasshook__method is inspecting the__annotations__dictionary ofotherto see if it can find any protocol members in that dictionary. But we know that there can't be any protocol members in the__annotations__dictionary, because if there were, that would makeothera protocol with at least one non-callable member. If it's a protocol that has at least one non-callable member, the__subclasshook__method is never called at all duringisinstance()orissubclass()checks, because we raiseTypeErrorin_ProtocolMeta.__subclasscheck__, short-circuiting the call toabc.ABCMeta.__subclasscheck__that would callProtocol.__subclasshook__:cpython/Lib/typing.py
Lines 1828 to 1831 in 3af2dc7
I believe that this block of code can therefore be safely deleted.
Linked PRs
issubclass()between two protocols #105835issubclass()between two protocols (GH-105835) #105859typing.Protocol(#105835) #105860