Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-112345: Let failed protocol subclasscheck show non-method members#112344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from5 commits
317d18a100c309fd840b2c70acc45db267071925d5998b45e77e3a4af2e3276bc40f8462b6f71File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4091,6 +4091,20 @@ 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 | ||
| expected_error_message = ( | ||
| "Protocols with non-method members don't support issubclass()." | ||
| " Non-method members: {'x', 'y'}." | ||
| ) | ||
| self.assertRaisesRegex(TypeError, f'^{re.escape(expected_error_message)}$') | ||
randolf-scholz marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| self.assertEqual(Vec2D.__protocol_attrs__, {'x', 'y', 'square_norm'}) | ||
| class GenericTests(BaseTestCase): | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 = { | ||
AlexWaygood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| 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()." | ||
| f" Non-method members: {non_method_attrs}." | ||
randolf-scholz marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ) | ||
| if not getattr(cls, '_is_runtime_protocol', False): | ||
| raise TypeError( | ||
randolf-scholz marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Improve error message when trying to call :func:`issubclass` against a :class:`typing.Protocol` that has non-method members. | ||
| Patch by Randolf Scholz. |