Skip to content

Commit

Permalink
gh-112345: typing.Protocol: Let failed subclasscheck show non-metho…
Browse files Browse the repository at this point in the history
…d members (#112344)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
  • Loading branch information
randolf-scholz and AlexWaygood committed Nov 24, 2023
1 parent d9fc152 commit e9d1360
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4091,6 +4091,22 @@ def method(self) -> None: ...
self.assertIsInstance(Foo(), ProtocolWithMixedMembers)
self.assertNotIsInstance(42, ProtocolWithMixedMembers)

def test_protocol_issubclass_error_message(self):
class Vec2D(Protocol):
x: float
y: float

def square_norm(self) -> float:
return self.x ** 2 + self.y ** 2

self.assertEqual(Vec2D.__protocol_attrs__, {'x', 'y', 'square_norm'})
expected_error_message = (
"Protocols with non-method members don't support issubclass()."
" Non-method members: 'x', 'y'."
)
with self.assertRaisesRegex(TypeError, re.escape(expected_error_message)):
issubclass(int, Vec2D)


class GenericTests(BaseTestCase):

Expand Down
7 changes: 6 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1828,8 +1828,13 @@ def __subclasscheck__(cls, other):
not cls.__callable_proto_members_only__
and cls.__dict__.get("__subclasshook__") is _proto_hook
):
non_method_attrs = sorted(
attr for attr in cls.__protocol_attrs__
if not callable(getattr(cls, attr, None))
)
raise TypeError(
"Protocols with non-method members don't support issubclass()"
"Protocols with non-method members don't support issubclass()."
f" Non-method members: {str(non_method_attrs)[1:-1]}."
)
if not getattr(cls, '_is_runtime_protocol', False):
raise TypeError(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve error message when trying to call :func:`issubclass` against a
:class:`typing.Protocol` that has non-method members.
Patch by Randolf Scholz.

0 comments on commit e9d1360

Please sign in to comment.