@@ -3349,6 +3349,71 @@ def __init__(self):
33493349
33503350 Foo () # Previously triggered RecursionError
33513351
3352+ def test_empty_protocol_decorated_with_final (self ):
3353+ @final
3354+ @runtime_checkable
3355+ class EmptyProtocol (Protocol ): ...
3356+
3357+ self .assertIsSubclass (object , EmptyProtocol )
3358+ self .assertIsInstance (object (), EmptyProtocol )
3359+
3360+ def test_protocol_decorated_with_final_callable_members (self ):
3361+ @final
3362+ @runtime_checkable
3363+ class ProtocolWithMethod (Protocol ):
3364+ def startswith (self , string : str ) -> bool : ...
3365+
3366+ self .assertIsSubclass (str , ProtocolWithMethod )
3367+ self .assertNotIsSubclass (int , ProtocolWithMethod )
3368+ self .assertIsInstance ('foo' , ProtocolWithMethod )
3369+ self .assertNotIsInstance (42 , ProtocolWithMethod )
3370+
3371+ def test_protocol_decorated_with_final_noncallable_members (self ):
3372+ @final
3373+ @runtime_checkable
3374+ class ProtocolWithNonCallableMember (Protocol ):
3375+ x : int
3376+
3377+ class Foo :
3378+ x = 42
3379+
3380+ only_callable_members_please = (
3381+ r"Protocols with non-method members don't support issubclass()"
3382+ )
3383+
3384+ with self .assertRaisesRegex (TypeError , only_callable_members_please ):
3385+ issubclass (Foo , ProtocolWithNonCallableMember )
3386+
3387+ with self .assertRaisesRegex (TypeError , only_callable_members_please ):
3388+ issubclass (int , ProtocolWithNonCallableMember )
3389+
3390+ self .assertIsInstance (Foo (), ProtocolWithNonCallableMember )
3391+ self .assertNotIsInstance (42 , ProtocolWithNonCallableMember )
3392+
3393+ def test_protocol_decorated_with_final_mixed_members (self ):
3394+ @final
3395+ @runtime_checkable
3396+ class ProtocolWithMixedMembers (Protocol ):
3397+ x : int
3398+ def method (self ) -> None : ...
3399+
3400+ class Foo :
3401+ x = 42
3402+ def method (self ) -> None : ...
3403+
3404+ only_callable_members_please = (
3405+ r"Protocols with non-method members don't support issubclass()"
3406+ )
3407+
3408+ with self .assertRaisesRegex (TypeError , only_callable_members_please ):
3409+ issubclass (Foo , ProtocolWithMixedMembers )
3410+
3411+ with self .assertRaisesRegex (TypeError , only_callable_members_please ):
3412+ issubclass (int , ProtocolWithMixedMembers )
3413+
3414+ self .assertIsInstance (Foo (), ProtocolWithMixedMembers )
3415+ self .assertNotIsInstance (42 , ProtocolWithMixedMembers )
3416+
33523417
33533418class GenericTests (BaseTestCase ):
33543419
0 commit comments