-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Backport CPython PR 26067 #132
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
587d843
Backport CPython PR 26067
AlexWaygood 8ab4bdb
See if this fixes things
AlexWaygood afbafbb
oops
AlexWaygood 376e9c2
.
AlexWaygood 53155cd
Merge branch 'main' into backport-26067
AlexWaygood 64f059b
.
AlexWaygood 30f4c83
.
AlexWaygood b507d3c
Fix tests on 3.9
AlexWaygood f77322c
Allow typing_extensions.runtime_checkable to decorate typing.Protocol
AlexWaygood 982efce
Fix flake8 errors
AlexWaygood be9940d
Merge remote-tracking branch 'upstream/main' into backport-26067
AlexWaygood ee378c7
Merge branch 'main' into backport-26067
AlexWaygood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -398,21 +398,33 @@ def clear_overloads(): | |
} | ||
|
||
|
||
_EXCLUDED_ATTRS = { | ||
"__abstractmethods__", "__annotations__", "__weakref__", "_is_protocol", | ||
"_is_runtime_protocol", "__dict__", "__slots__", "__parameters__", | ||
"__orig_bases__", "__module__", "_MutableMapping__marker", "__doc__", | ||
"__subclasshook__", "__orig_class__", "__init__", "__new__", | ||
} | ||
|
||
if sys.version_info < (3, 8): | ||
_EXCLUDED_ATTRS |= { | ||
"_gorg", "__next_in_mro__", "__extra__", "__tree_hash__", "__args__", | ||
"__origin__" | ||
} | ||
|
||
if sys.version_info >= (3, 9): | ||
_EXCLUDED_ATTRS.add("__class_getitem__") | ||
|
||
_EXCLUDED_ATTRS = frozenset(_EXCLUDED_ATTRS) | ||
|
||
|
||
def _get_protocol_attrs(cls): | ||
attrs = set() | ||
for base in cls.__mro__[:-1]: # without object | ||
if base.__name__ in ('Protocol', 'Generic'): | ||
continue | ||
annotations = getattr(base, '__annotations__', {}) | ||
for attr in list(base.__dict__.keys()) + list(annotations.keys()): | ||
if (not attr.startswith('_abc_') and attr not in ( | ||
'__abstractmethods__', '__annotations__', '__weakref__', | ||
'_is_protocol', '_is_runtime_protocol', '__dict__', | ||
'__args__', '__slots__', | ||
'__next_in_mro__', '__parameters__', '__origin__', | ||
'__orig_bases__', '__extra__', '__tree_hash__', | ||
'__doc__', '__subclasshook__', '__init__', '__new__', | ||
'__module__', '_MutableMapping__marker', '_gorg')): | ||
if (not attr.startswith('_abc_') and attr not in _EXCLUDED_ATTRS): | ||
attrs.add(attr) | ||
return attrs | ||
|
||
|
@@ -468,11 +480,18 @@ def _caller(depth=2): | |
return None | ||
|
||
|
||
# 3.8+ | ||
if hasattr(typing, 'Protocol'): | ||
# A bug in runtime-checkable protocols was fixed in 3.10+, | ||
# but we backport it to all versions | ||
if sys.version_info >= (3, 10): | ||
Protocol = typing.Protocol | ||
# 3.7 | ||
runtime_checkable = typing.runtime_checkable | ||
else: | ||
def _allow_reckless_class_checks(depth=4): | ||
"""Allow instance and class checks for special stdlib modules. | ||
The abc and functools modules indiscriminately call isinstance() and | ||
issubclass() on the whole MRO of a user class, which may contain protocols. | ||
""" | ||
return _caller(depth) in {'abc', 'functools', None} | ||
|
||
def _no_init(self, *args, **kwargs): | ||
if type(self)._is_protocol: | ||
|
@@ -484,11 +503,19 @@ class _ProtocolMeta(abc.ABCMeta): | |
def __instancecheck__(cls, instance): | ||
# We need this method for situations where attributes are | ||
# assigned in __init__. | ||
if ((not getattr(cls, '_is_protocol', False) or | ||
is_protocol_cls = getattr(cls, "_is_protocol", False) | ||
if ( | ||
is_protocol_cls and | ||
not getattr(cls, '_is_runtime_protocol', False) and | ||
not _allow_reckless_class_checks(depth=2) | ||
): | ||
raise TypeError("Instance and class checks can only be used with" | ||
" @runtime_checkable protocols") | ||
if ((not is_protocol_cls or | ||
_is_callable_members_only(cls)) and | ||
issubclass(instance.__class__, cls)): | ||
return True | ||
if cls._is_protocol: | ||
if is_protocol_cls: | ||
if all(hasattr(instance, attr) and | ||
(not callable(getattr(cls, attr, None)) or | ||
getattr(instance, attr) is not None) | ||
|
@@ -530,6 +557,7 @@ def meth(self) -> T: | |
""" | ||
__slots__ = () | ||
_is_protocol = True | ||
_is_runtime_protocol = False | ||
|
||
def __new__(cls, *args, **kwds): | ||
if cls is Protocol: | ||
|
@@ -581,12 +609,12 @@ def _proto_hook(other): | |
if not cls.__dict__.get('_is_protocol', None): | ||
return NotImplemented | ||
if not getattr(cls, '_is_runtime_protocol', False): | ||
if _caller(depth=3) in {'abc', 'functools'}: | ||
if _allow_reckless_class_checks(): | ||
return NotImplemented | ||
raise TypeError("Instance and class checks can only be used with" | ||
" @runtime protocols") | ||
if not _is_callable_members_only(cls): | ||
if _caller(depth=3) in {'abc', 'functools'}: | ||
if _allow_reckless_class_checks(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
return NotImplemented | ||
raise TypeError("Protocols with non-method members" | ||
" don't support issubclass()") | ||
|
@@ -625,12 +653,6 @@ def _proto_hook(other): | |
f' protocols, got {repr(base)}') | ||
cls.__init__ = _no_init | ||
|
||
|
||
# 3.8+ | ||
if hasattr(typing, 'runtime_checkable'): | ||
runtime_checkable = typing.runtime_checkable | ||
# 3.7 | ||
else: | ||
def runtime_checkable(cls): | ||
"""Mark a protocol class as a runtime protocol, so that it | ||
can be used with isinstance() and issubclass(). Raise TypeError | ||
|
@@ -639,7 +661,10 @@ def runtime_checkable(cls): | |
This allows a simple-minded structural check very similar to the | ||
one-offs in collections.abc such as Hashable. | ||
""" | ||
if not isinstance(cls, _ProtocolMeta) or not cls._is_protocol: | ||
if not ( | ||
(isinstance(cls, _ProtocolMeta) or issubclass(cls, typing.Generic)) | ||
and getattr(cls, "_is_protocol", False) | ||
): | ||
raise TypeError('@runtime_checkable can be only applied to protocol classes,' | ||
f' got {cls!r}') | ||
cls._is_runtime_protocol = True | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it correct that the depth is changed from 3 to 4?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because by adding a new function that the code has to pass through before it gets to the
sys._getframe
call, the call stack becomes "another frame deep"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could just have looked at
_caller
and figured that out myself. 🤦