mirror of
https://github.com/python/cpython.git
synced 2025-08-08 02:48:55 +00:00
[3.12] gh-104935: typing: Fix interactions between @runtime_checkable
and Generic
(GH-104939) (#104941)
gh-104935: typing: Fix interactions between `@runtime_checkable` and `Generic` (GH-104939)
---------
(cherry picked from commit 2b7027d0b2
)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
d176f78ec2
commit
930efde4c7
3 changed files with 48 additions and 3 deletions
|
@ -1899,7 +1899,7 @@ class Protocol(Generic, metaclass=_ProtocolMeta):
|
|||
annotations = getattr(base, '__annotations__', {})
|
||||
if (isinstance(annotations, collections.abc.Mapping) and
|
||||
attr in annotations and
|
||||
issubclass(other, Generic) and other._is_protocol):
|
||||
issubclass(other, Generic) and getattr(other, '_is_protocol', False)):
|
||||
break
|
||||
else:
|
||||
return NotImplemented
|
||||
|
@ -1917,7 +1917,7 @@ class Protocol(Generic, metaclass=_ProtocolMeta):
|
|||
if not (base in (object, Generic) or
|
||||
base.__module__ in _PROTO_ALLOWLIST and
|
||||
base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
|
||||
issubclass(base, Generic) and base._is_protocol):
|
||||
issubclass(base, Generic) and getattr(base, '_is_protocol', False)):
|
||||
raise TypeError('Protocols can only inherit from other'
|
||||
' protocols, got %r' % base)
|
||||
if cls.__init__ is Protocol.__init__:
|
||||
|
@ -2064,7 +2064,7 @@ def runtime_checkable(cls):
|
|||
Warning: this will check only the presence of the required methods,
|
||||
not their type signatures!
|
||||
"""
|
||||
if not issubclass(cls, Generic) or not cls._is_protocol:
|
||||
if not issubclass(cls, Generic) or not getattr(cls, '_is_protocol', False):
|
||||
raise TypeError('@runtime_checkable can be only applied to protocol classes,'
|
||||
' got %r' % cls)
|
||||
cls._is_runtime_protocol = True
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue