@@ -1775,8 +1775,8 @@ def _pickle_pskwargs(pskwargs):
1775
1775
1776
1776
1777
1777
class _ProtocolMeta (ABCMeta ):
1778
- # This metaclass is really unfortunate and exists only because of
1779
- # the lack of __instancehook__ .
1778
+ # This metaclass is somewhat unfortunate,
1779
+ # but is necessary for several reasons.. .
1780
1780
def __init__ (cls , * args , ** kwargs ):
1781
1781
super ().__init__ (* args , ** kwargs )
1782
1782
cls .__protocol_attrs__ = _get_protocol_attrs (cls )
@@ -1786,6 +1786,17 @@ def __init__(cls, *args, **kwargs):
1786
1786
callable (getattr (cls , attr , None )) for attr in cls .__protocol_attrs__
1787
1787
)
1788
1788
1789
+ def __subclasscheck__ (cls , other ):
1790
+ if (
1791
+ getattr (cls , '_is_protocol' , False )
1792
+ and not cls .__callable_proto_members_only__
1793
+ and not _allow_reckless_class_checks (depth = 2 )
1794
+ ):
1795
+ raise TypeError (
1796
+ "Protocols with non-method members don't support issubclass()"
1797
+ )
1798
+ return super ().__subclasscheck__ (other )
1799
+
1789
1800
def __instancecheck__ (cls , instance ):
1790
1801
# We need this method for situations where attributes are
1791
1802
# assigned in __init__.
@@ -1798,17 +1809,10 @@ def __instancecheck__(cls, instance):
1798
1809
raise TypeError ("Instance and class checks can only be used with"
1799
1810
" @runtime_checkable protocols" )
1800
1811
1801
- # gh-104555: Don't call super().__instancecheck__ here,
1802
- # ABCMeta.__instancecheck__ would erroneously use it to populate the cache,
1803
- # which would cause incorrect results for *issubclass()* calls
1804
- if type .__instancecheck__ (cls , instance ):
1812
+ if super ().__instancecheck__ (instance ):
1805
1813
return True
1806
1814
1807
1815
if is_protocol_cls :
1808
- # Fast path for protocols with only callable members
1809
- if cls .__callable_proto_members_only__ and issubclass (type (instance ), cls ):
1810
- return True
1811
-
1812
1816
getattr_static = _lazy_load_getattr_static ()
1813
1817
for attr in cls .__protocol_attrs__ :
1814
1818
try :
@@ -1820,7 +1824,7 @@ def __instancecheck__(cls, instance):
1820
1824
else :
1821
1825
return True
1822
1826
1823
- return super (). __instancecheck__ ( instance )
1827
+ return False
1824
1828
1825
1829
1826
1830
class Protocol (Generic , metaclass = _ProtocolMeta ):
@@ -1876,11 +1880,6 @@ def _proto_hook(other):
1876
1880
raise TypeError ("Instance and class checks can only be used with"
1877
1881
" @runtime_checkable protocols" )
1878
1882
1879
- if not cls .__callable_proto_members_only__ :
1880
- if _allow_reckless_class_checks ():
1881
- return NotImplemented
1882
- raise TypeError ("Protocols with non-method members"
1883
- " don't support issubclass()" )
1884
1883
if not isinstance (other , type ):
1885
1884
# Same error message as for issubclass(1, int).
1886
1885
raise TypeError ('issubclass() arg 1 must be a class' )
0 commit comments