@@ -2758,6 +2758,65 @@ def x(self): ...
2758
2758
with self .assertRaisesRegex (TypeError , only_classes_allowed ):
2759
2759
issubclass (1 , BadPG )
2760
2760
2761
+ def test_issubclass_and_isinstance_on_Protocol_itself (self ):
2762
+ class C :
2763
+ def x (self ): pass
2764
+
2765
+ self .assertNotIsSubclass (object , Protocol )
2766
+ self .assertNotIsInstance (object (), Protocol )
2767
+
2768
+ self .assertNotIsSubclass (str , Protocol )
2769
+ self .assertNotIsInstance ('foo' , Protocol )
2770
+
2771
+ self .assertNotIsSubclass (C , Protocol )
2772
+ self .assertNotIsInstance (C (), Protocol )
2773
+
2774
+ only_classes_allowed = r"issubclass\(\) arg 1 must be a class"
2775
+
2776
+ with self .assertRaisesRegex (TypeError , only_classes_allowed ):
2777
+ issubclass (1 , Protocol )
2778
+ with self .assertRaisesRegex (TypeError , only_classes_allowed ):
2779
+ issubclass ('foo' , Protocol )
2780
+ with self .assertRaisesRegex (TypeError , only_classes_allowed ):
2781
+ issubclass (C (), Protocol )
2782
+
2783
+ T = TypeVar ('T' )
2784
+
2785
+ @runtime_checkable
2786
+ class EmptyProtocol (Protocol ): pass
2787
+
2788
+ @runtime_checkable
2789
+ class SupportsStartsWith (Protocol ):
2790
+ def startswith (self , x : str ) -> bool : ...
2791
+
2792
+ @runtime_checkable
2793
+ class SupportsX (Protocol [T ]):
2794
+ def x (self ): ...
2795
+
2796
+ for proto in EmptyProtocol , SupportsStartsWith , SupportsX :
2797
+ with self .subTest (proto = proto .__name__ ):
2798
+ self .assertIsSubclass (proto , Protocol )
2799
+
2800
+ # gh-105237 / PR #105239:
2801
+ # check that the presence of Protocol subclasses
2802
+ # where `issubclass(X, <subclass>)` evaluates to True
2803
+ # doesn't influence the result of `issubclass(X, Protocol)`
2804
+
2805
+ self .assertIsSubclass (object , EmptyProtocol )
2806
+ self .assertIsInstance (object (), EmptyProtocol )
2807
+ self .assertNotIsSubclass (object , Protocol )
2808
+ self .assertNotIsInstance (object (), Protocol )
2809
+
2810
+ self .assertIsSubclass (str , SupportsStartsWith )
2811
+ self .assertIsInstance ('foo' , SupportsStartsWith )
2812
+ self .assertNotIsSubclass (str , Protocol )
2813
+ self .assertNotIsInstance ('foo' , Protocol )
2814
+
2815
+ self .assertIsSubclass (C , SupportsX )
2816
+ self .assertIsInstance (C (), SupportsX )
2817
+ self .assertNotIsSubclass (C , Protocol )
2818
+ self .assertNotIsInstance (C (), Protocol )
2819
+
2761
2820
def test_protocols_issubclass_non_callable (self ):
2762
2821
class C :
2763
2822
x = 1
0 commit comments