@@ -1790,6 +1790,23 @@ def _pickle_pskwargs(pskwargs):
17901790_abc_subclasscheck = ABCMeta .__subclasscheck__
17911791
17921792
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+
17931810class _ProtocolMeta (ABCMeta ):
17941811 # This metaclass is somewhat unfortunate,
17951812 # but is necessary for several reasons...
@@ -1829,13 +1846,11 @@ def __subclasscheck__(cls, other):
18291846 getattr (cls , '_is_protocol' , False )
18301847 and not _allow_reckless_class_checks ()
18311848 ):
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' )
18351849 if (
18361850 not cls .__callable_proto_members_only__
18371851 and cls .__dict__ .get ("__subclasshook__" ) is _proto_hook
18381852 ):
1853+ _type_check_issubclass_arg_1 (other )
18391854 non_method_attrs = sorted (
18401855 attr for attr in cls .__protocol_attrs__
18411856 if not callable (getattr (cls , attr , None ))
@@ -1845,6 +1860,7 @@ def __subclasscheck__(cls, other):
18451860 f" Non-method members: { str (non_method_attrs )[1 :- 1 ]} ."
18461861 )
18471862 if not getattr (cls , '_is_runtime_protocol' , False ):
1863+ _type_check_issubclass_arg_1 (other )
18481864 raise TypeError (
18491865 "Instance and class checks can only be used with "
18501866 "@runtime_checkable protocols"
0 commit comments