mirror of
https://github.com/python/cpython.git
synced 2025-08-08 02:48:55 +00:00
[3.12] gh-105237: Allow calling issubclass(X, typing.Protocol)
again (GH-105239) (#105316)
gh-105237: Allow calling `issubclass(X, typing.Protocol)` again (GH-105239)
(cherry picked from commit cdfb201bfa
)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
6d0354167f
commit
51750269cf
3 changed files with 65 additions and 0 deletions
|
@ -1788,6 +1788,8 @@ class _ProtocolMeta(ABCMeta):
|
|||
)
|
||||
|
||||
def __subclasscheck__(cls, other):
|
||||
if cls is Protocol:
|
||||
return type.__subclasscheck__(cls, other)
|
||||
if not isinstance(other, type):
|
||||
# Same error message as for issubclass(1, int).
|
||||
raise TypeError('issubclass() arg 1 must be a class')
|
||||
|
@ -1809,6 +1811,8 @@ class _ProtocolMeta(ABCMeta):
|
|||
def __instancecheck__(cls, instance):
|
||||
# We need this method for situations where attributes are
|
||||
# assigned in __init__.
|
||||
if cls is Protocol:
|
||||
return type.__instancecheck__(cls, instance)
|
||||
if not getattr(cls, "_is_protocol", False):
|
||||
# i.e., it's a concrete subclass of a protocol
|
||||
return super().__instancecheck__(instance)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue