File tree 4 files changed +18
-1
lines changed 4 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -484,7 +484,13 @@ def test_reversible(self):
484
484
def test_protocol_instance_type_error (self ):
485
485
with self .assertRaises (TypeError ):
486
486
isinstance (0 , typing .SupportsAbs )
487
-
487
+ class C1 (typing .SupportsInt ):
488
+ def __int__ (self ):
489
+ return 42
490
+ class C2 (C1 ):
491
+ pass
492
+ c = C2 ()
493
+ self .assertIsInstance (c , C1 )
488
494
489
495
class GenericTests (BaseTestCase ):
490
496
Original file line number Diff line number Diff line change @@ -1462,6 +1462,8 @@ class _ProtocolMeta(GenericMeta):
1462
1462
"""
1463
1463
1464
1464
def __instancecheck__ (self , obj ):
1465
+ if _Protocol not in self .__bases__ :
1466
+ return super (_ProtocolMeta , self ).__instancecheck__ (obj )
1465
1467
raise TypeError ("Protocols cannot be used with isinstance()." )
1466
1468
1467
1469
def __subclasscheck__ (self , cls ):
Original file line number Diff line number Diff line change @@ -513,6 +513,13 @@ def test_reversible(self):
513
513
def test_protocol_instance_type_error (self ):
514
514
with self .assertRaises (TypeError ):
515
515
isinstance (0 , typing .SupportsAbs )
516
+ class C1 (typing .SupportsInt ):
517
+ def __int__ (self ) -> int :
518
+ return 42
519
+ class C2 (C1 ):
520
+ pass
521
+ c = C2 ()
522
+ self .assertIsInstance (c , C1 )
516
523
517
524
518
525
class GenericTests (BaseTestCase ):
Original file line number Diff line number Diff line change @@ -1503,6 +1503,8 @@ class _ProtocolMeta(GenericMeta):
1503
1503
"""
1504
1504
1505
1505
def __instancecheck__ (self , obj ):
1506
+ if _Protocol not in self .__bases__ :
1507
+ return super ().__instancecheck__ (obj )
1506
1508
raise TypeError ("Protocols cannot be used with isinstance()." )
1507
1509
1508
1510
def __subclasscheck__ (self , cls ):
You can’t perform that action at this time.
0 commit comments