From 7ac3ebf4f8ba0914c62d73b5b3b5a85c1a3c79be Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 12 Apr 2024 09:20:54 +0300 Subject: [PATCH 1/3] gh-117797: Improve `test_descr.test_not_implemented` --- Lib/test/test_descr.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 097ca38e0b1ed8..6d82ac23a16684 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4594,18 +4594,16 @@ def test_special_unbound_method_types(self): def test_not_implemented(self): # Testing NotImplemented... # all binary methods should be able to return a NotImplemented - import operator def specialmethod(self, other): return NotImplemented def check(expr, x, y): - try: - exec(expr, {'x': x, 'y': y, 'operator': operator}) - except TypeError: - pass - else: - self.fail("no TypeError from %r" % (expr,)) + with ( + self.subTest(expr=expr, x=x, y=y), + self.assertRaises(TypeError) + ): + exec(expr, {'x': x, 'y': y}) N1 = sys.maxsize + 1 # might trigger OverflowErrors instead of # TypeErrors @@ -4632,6 +4630,15 @@ def check(expr, x, y): check(expr, a, a) check(expr, a, N1) check(expr, a, N2) + B = type('B', (), {rname: specialmethod}) + b = B() + check(expr, b, b) + check(expr, a, b) + check(expr, b, a) + check(expr, b, N1) + check(expr, b, N2) + check(expr, N1, b) + check(expr, N2, b) if iexpr: check(iexpr, a, a) check(iexpr, a, N1) From a62d8bb5115a4c3d337207ba136d751b9b0b893b Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 16 Apr 2024 10:39:54 +0300 Subject: [PATCH 2/3] Address review --- Lib/test/test_descr.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 6d82ac23a16684..953cd7895001ce 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4624,12 +4624,14 @@ def check(expr, x, y): ('__and__', 'x & y', 'x &= y'), ('__or__', 'x | y', 'x |= y'), ('__xor__', 'x ^ y', 'x ^= y')]: - rname = '__r' + name[2:] + # Defined 'left' magic method: A = type('A', (), {name: specialmethod}) a = A() check(expr, a, a) check(expr, a, N1) check(expr, a, N2) + # Defines 'right' magic method: + rname = '__r' + name[2:] B = type('B', (), {rname: specialmethod}) b = B() check(expr, b, b) From 251f430051c33cb9bf27b6cd4ddcf41d2a3b6bdf Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 16 Apr 2024 10:52:45 +0300 Subject: [PATCH 3/3] Typo! --- Lib/test/test_descr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 953cd7895001ce..af1e0eac5c3d72 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4601,7 +4601,7 @@ def specialmethod(self, other): def check(expr, x, y): with ( self.subTest(expr=expr, x=x, y=y), - self.assertRaises(TypeError) + self.assertRaises(TypeError), ): exec(expr, {'x': x, 'y': y}) @@ -4624,7 +4624,7 @@ def check(expr, x, y): ('__and__', 'x & y', 'x &= y'), ('__or__', 'x | y', 'x |= y'), ('__xor__', 'x ^ y', 'x ^= y')]: - # Defined 'left' magic method: + # Defines 'left' magic method: A = type('A', (), {name: specialmethod}) a = A() check(expr, a, a)