@@ -1790,6 +1790,23 @@ def _pickle_pskwargs(pskwargs):
1790
1790
_abc_subclasscheck = ABCMeta .__subclasscheck__
1791
1791
1792
1792
1793
+ def _type_check_issubclass_arg_1 (arg ):
1794
+ """Raise TypeError if `arg` is not an instance of `type`
1795
+ in `issubclass(arg, <protocol>)`.
1796
+
1797
+ In most cases, this is verified by type.__subclasscheck__.
1798
+ Checking it again unnecessarily would slow down issubclass() checks,
1799
+ so, we don't perform this check unless we absolutely have to.
1800
+
1801
+ For various error paths, however,
1802
+ we want to ensure that *this* error message is shown to the user
1803
+ where relevant, rather than a typing.py-specific error message.
1804
+ """
1805
+ if not isinstance (arg , type ):
1806
+ # Same error message as for issubclass(1, int).
1807
+ raise TypeError ('issubclass() arg 1 must be a class' )
1808
+
1809
+
1793
1810
class _ProtocolMeta (ABCMeta ):
1794
1811
# This metaclass is somewhat unfortunate,
1795
1812
# but is necessary for several reasons...
@@ -1829,13 +1846,11 @@ def __subclasscheck__(cls, other):
1829
1846
getattr (cls , '_is_protocol' , False )
1830
1847
and not _allow_reckless_class_checks ()
1831
1848
):
1832
- if not isinstance (other , type ):
1833
- # Same error message as for issubclass(1, int).
1834
- raise TypeError ('issubclass() arg 1 must be a class' )
1835
1849
if (
1836
1850
not cls .__callable_proto_members_only__
1837
1851
and cls .__dict__ .get ("__subclasshook__" ) is _proto_hook
1838
1852
):
1853
+ _type_check_issubclass_arg_1 (other )
1839
1854
non_method_attrs = sorted (
1840
1855
attr for attr in cls .__protocol_attrs__
1841
1856
if not callable (getattr (cls , attr , None ))
@@ -1845,6 +1860,7 @@ def __subclasscheck__(cls, other):
1845
1860
f" Non-method members: { str (non_method_attrs )[1 :- 1 ]} ."
1846
1861
)
1847
1862
if not getattr (cls , '_is_runtime_protocol' , False ):
1863
+ _type_check_issubclass_arg_1 (other )
1848
1864
raise TypeError (
1849
1865
"Instance and class checks can only be used with "
1850
1866
"@runtime_checkable protocols"
0 commit comments