Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-105144: Runtime-checkable protocols: move all 'sanity checks' to _ProtocolMeta.__subclasscheck__#105152
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AlexWaygood merged 4 commits into python:main from AlexWaygood:fix-bizarre-subclassing-bugMay 31, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
gh-105144: Runtime-checkable protocols: move all 'sanity checks' to _ProtocolMeta.__subclasscheck__#105152
Changes from all commits
Commits
Show all changes
4 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1727,7 +1727,7 @@ def _caller(depth=1, default='__main__'): | ||
| pass | ||
| return None | ||
| def _allow_reckless_class_checks(depth=3): | ||
| def _allow_reckless_class_checks(depth=2): | ||
| """Allow instance and class checks for special stdlib modules. | ||
| The abc and functools modules indiscriminately call isinstance() and | ||
| @@ -1782,14 +1782,22 @@ def __init__(cls, *args, **kwargs): | ||
| ) | ||
| def __subclasscheck__(cls, other): | ||
| if not isinstance(other, type): | ||
JelleZijlstra marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| # Same error message as for issubclass(1, int). | ||
| raise TypeError('issubclass() arg 1 must be a class') | ||
| if ( | ||
| getattr(cls, '_is_protocol', False) | ||
| and not cls.__callable_proto_members_only__ | ||
| and not _allow_reckless_class_checks(depth=2) | ||
| and not _allow_reckless_class_checks() | ||
| ): | ||
| raise TypeError( | ||
| "Protocols with non-method members don't support issubclass()" | ||
| ) | ||
| if not cls.__callable_proto_members_only__: | ||
| raise TypeError( | ||
| "Protocols with non-method members don't support issubclass()" | ||
| ) | ||
| if not getattr(cls, '_is_runtime_protocol', False): | ||
| raise TypeError( | ||
| "Instance and class checks can only be used with " | ||
| "@runtime_checkable protocols" | ||
| ) | ||
| return super().__subclasscheck__(other) | ||
| def __instancecheck__(cls, instance): | ||
| @@ -1801,7 +1809,7 @@ def __instancecheck__(cls, instance): | ||
| if ( | ||
| not getattr(cls, '_is_runtime_protocol', False) and | ||
| not _allow_reckless_class_checks(depth=2) | ||
| not _allow_reckless_class_checks() | ||
| ): | ||
| raise TypeError("Instance and class checks can only be used with" | ||
| " @runtime_checkable protocols") | ||
| @@ -1869,18 +1877,6 @@ def _proto_hook(other): | ||
| if not cls.__dict__.get('_is_protocol', False): | ||
| return NotImplemented | ||
| # First, perform various sanity checks. | ||
| if not getattr(cls, '_is_runtime_protocol', False): | ||
| if _allow_reckless_class_checks(): | ||
| return NotImplemented | ||
| raise TypeError("Instance and class checks can only be used with" | ||
| " @runtime_checkable protocols") | ||
| if not isinstance(other, type): | ||
| # Same error message as for issubclass(1, int). | ||
| raise TypeError('issubclass() arg 1 must be a class') | ||
| # Second, perform the actual structural compatibility check. | ||
| for attr in cls.__protocol_attrs__: | ||
| for base in other.__mro__: | ||
| # Check if the members appears in the class dictionary... | ||
5 changes: 5 additions & 0 deletions 5 Misc/NEWS.d/next/Library/2023-05-31-16-58-42.gh-issue-105144.Oqfn0V.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Fix a recent regression in the :mod:`typing` module. The regression meant | ||
| that doing ``class Foo(X, typing.Protocol)``, where ``X`` was a class that | ||
| had :class:`abc.ABCMeta` as its metaclass, would then cause subsequent | ||
| ``isinstance(1, X)`` calls to erroneously raise :exc:`TypeError`. Patch by | ||
| Alex Waygood. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.