@@ -1782,6 +1782,14 @@ def _pickle_pskwargs(pskwargs):
1782
1782
del _pickle_psargs , _pickle_pskwargs
1783
1783
1784
1784
1785
+ # Preload these once, as globals, as a micro-optimisation.
1786
+ # This makes a significant difference to the time it takes
1787
+ # to do `isinstance()`/`issubclass()` checks
1788
+ # against runtime-checkable protocols with only one callable member.
1789
+ _abc_instancecheck = ABCMeta .__instancecheck__
1790
+ _abc_subclasscheck = ABCMeta .__subclasscheck__
1791
+
1792
+
1785
1793
class _ProtocolMeta (ABCMeta ):
1786
1794
# This metaclass is somewhat unfortunate,
1787
1795
# but is necessary for several reasons...
@@ -1841,7 +1849,7 @@ def __subclasscheck__(cls, other):
1841
1849
"Instance and class checks can only be used with "
1842
1850
"@runtime_checkable protocols"
1843
1851
)
1844
- return super (). __subclasscheck__ ( other )
1852
+ return _abc_subclasscheck ( cls , other )
1845
1853
1846
1854
def __instancecheck__ (cls , instance ):
1847
1855
# We need this method for situations where attributes are
@@ -1850,7 +1858,7 @@ def __instancecheck__(cls, instance):
1850
1858
return type .__instancecheck__ (cls , instance )
1851
1859
if not getattr (cls , "_is_protocol" , False ):
1852
1860
# i.e., it's a concrete subclass of a protocol
1853
- return super (). __instancecheck__ ( instance )
1861
+ return _abc_instancecheck ( cls , instance )
1854
1862
1855
1863
if (
1856
1864
not getattr (cls , '_is_runtime_protocol' , False ) and
@@ -1859,7 +1867,7 @@ def __instancecheck__(cls, instance):
1859
1867
raise TypeError ("Instance and class checks can only be used with"
1860
1868
" @runtime_checkable protocols" )
1861
1869
1862
- if super (). __instancecheck__ ( instance ):
1870
+ if _abc_instancecheck ( cls , instance ):
1863
1871
return True
1864
1872
1865
1873
getattr_static = _lazy_load_getattr_static ()
0 commit comments