-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
Description
Bug report
If you apply this diff to typing.py, all tests continue to pass:
diff --git a/Lib/typing.py b/Lib/typing.py
index 1dd9398344..98e19644a2 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1931,6 +1931,7 @@ def _proto_hook(other):
if (isinstance(annotations, collections.abc.Mapping) and
attr in annotations and
issubclass(other, Generic) and getattr(other, '_is_protocol', False)):
+ 1/0
breakThe break on line 1934 here is unreachable; thus, this whole block of code is pointless:
Lines 1929 to 1934 in 3af2dc7
| # ...or in annotations, if it is a sub-protocol. | |
| annotations = getattr(base, '__annotations__', {}) | |
| if (isinstance(annotations, collections.abc.Mapping) and | |
| attr in annotations and | |
| issubclass(other, Generic) and getattr(other, '_is_protocol', False)): | |
| break |
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__:
Lines 1828 to 1831 in 3af2dc7
| if not cls.__callable_proto_members_only__: | |
| raise TypeError( | |
| "Protocols with non-method members don't support issubclass()" | |
| ) |
I believe that this block of code can therefore be safely deleted.