@@ -2654,6 +2654,42 @@ class D(PNonCall): ...
2654
2654
with self .assertRaises (TypeError ):
2655
2655
issubclass (D , PNonCall )
2656
2656
2657
+ def test_no_weird_caching_with_issubclass_after_isinstance (self ):
2658
+ @runtime_checkable
2659
+ class Spam (Protocol [T ]):
2660
+ x : T
2661
+
2662
+ class Eggs (Generic [T ]):
2663
+ def __init__ (self , x : T ) -> None :
2664
+ self .x = x
2665
+
2666
+ # gh-104555: ABCMeta might cache the result of this isinstance check
2667
+ # if we called super().__instancecheck__ in the wrong place
2668
+ # in _ProtocolMeta.__instancheck__...
2669
+ self .assertIsInstance (Eggs (42 ), Spam )
2670
+
2671
+ # ...and if it did, then TypeError wouldn't be raised here!
2672
+ with self .assertRaises (TypeError ):
2673
+ issubclass (Eggs , Spam )
2674
+
2675
+ def test_no_weird_caching_with_issubclass_after_isinstance_pep695 (self ):
2676
+ @runtime_checkable
2677
+ class Spam [T ](Protocol ):
2678
+ x : T
2679
+
2680
+ class Eggs [T ]:
2681
+ def __init__ (self , x : T ) -> None :
2682
+ self .x = x
2683
+
2684
+ # gh-104555: ABCMeta might cache the result of this isinstance check
2685
+ # if we called super().__instancecheck__ in the wrong place
2686
+ # in _ProtocolMeta.__instancheck__...
2687
+ self .assertIsInstance (Eggs (42 ), Spam )
2688
+
2689
+ # ...and if it did, then TypeError wouldn't be raised here!
2690
+ with self .assertRaises (TypeError ):
2691
+ issubclass (Eggs , Spam )
2692
+
2657
2693
def test_protocols_isinstance (self ):
2658
2694
T = TypeVar ('T' )
2659
2695
0 commit comments