@@ -4594,18 +4594,16 @@ def test_special_unbound_method_types(self):
45944594 def test_not_implemented (self ):
45954595 # Testing NotImplemented...
45964596 # all binary methods should be able to return a NotImplemented
4597- import operator
45984597
45994598 def specialmethod (self , other ):
46004599 return NotImplemented
46014600
46024601 def check (expr , x , y ):
4603- try :
4604- exec (expr , {'x' : x , 'y' : y , 'operator' : operator })
4605- except TypeError :
4606- pass
4607- else :
4608- self .fail ("no TypeError from %r" % (expr ,))
4602+ with (
4603+ self .subTest (expr = expr , x = x , y = y ),
4604+ self .assertRaises (TypeError ),
4605+ ):
4606+ exec (expr , {'x' : x , 'y' : y })
46094607
46104608 N1 = sys .maxsize + 1 # might trigger OverflowErrors instead of
46114609 # TypeErrors
@@ -4626,12 +4624,23 @@ def check(expr, x, y):
46264624 ('__and__' , 'x & y' , 'x &= y' ),
46274625 ('__or__' , 'x | y' , 'x |= y' ),
46284626 ('__xor__' , 'x ^ y' , 'x ^= y' )]:
4629- rname = '__r' + name [ 2 :]
4627+ # Defines 'left' magic method:
46304628 A = type ('A' , (), {name : specialmethod })
46314629 a = A ()
46324630 check (expr , a , a )
46334631 check (expr , a , N1 )
46344632 check (expr , a , N2 )
4633+ # Defines 'right' magic method:
4634+ rname = '__r' + name [2 :]
4635+ B = type ('B' , (), {rname : specialmethod })
4636+ b = B ()
4637+ check (expr , b , b )
4638+ check (expr , a , b )
4639+ check (expr , b , a )
4640+ check (expr , b , N1 )
4641+ check (expr , b , N2 )
4642+ check (expr , N1 , b )
4643+ check (expr , N2 , b )
46354644 if iexpr :
46364645 check (iexpr , a , a )
46374646 check (iexpr , a , N1 )
0 commit comments