[3.12] gh-117797: Improve test_descr.test_not_implemented (GH-117798) (#117921)

gh-117797: Improve `test_descr.test_not_implemented` (GH-117798)
(cherry picked from commit 1a1e013a4a)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
Miss Islington (bot) 2024-04-16 11:50:10 +02:00 committed by GitHub
parent 556fb3675c
commit 44eab29cbd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4594,18 +4594,16 @@ order (MRO) for bases """
def test_not_implemented(self): def test_not_implemented(self):
# Testing NotImplemented... # Testing NotImplemented...
# all binary methods should be able to return a NotImplemented # all binary methods should be able to return a NotImplemented
import operator
def specialmethod(self, other): def specialmethod(self, other):
return NotImplemented return NotImplemented
def check(expr, x, y): def check(expr, x, y):
try: with (
exec(expr, {'x': x, 'y': y, 'operator': operator}) self.subTest(expr=expr, x=x, y=y),
except TypeError: self.assertRaises(TypeError),
pass ):
else: exec(expr, {'x': x, 'y': y})
self.fail("no TypeError from %r" % (expr,))
N1 = sys.maxsize + 1 # might trigger OverflowErrors instead of N1 = sys.maxsize + 1 # might trigger OverflowErrors instead of
# TypeErrors # TypeErrors
@ -4626,12 +4624,23 @@ order (MRO) for bases """
('__and__', 'x & y', 'x &= y'), ('__and__', 'x & y', 'x &= y'),
('__or__', 'x | y', 'x |= y'), ('__or__', 'x | y', 'x |= y'),
('__xor__', 'x ^ y', 'x ^= y')]: ('__xor__', 'x ^ y', 'x ^= y')]:
rname = '__r' + name[2:] # Defines 'left' magic method:
A = type('A', (), {name: specialmethod}) A = type('A', (), {name: specialmethod})
a = A() a = A()
check(expr, a, a) check(expr, a, a)
check(expr, a, N1) check(expr, a, N1)
check(expr, a, N2) check(expr, a, N2)
# Defines 'right' magic method:
rname = '__r' + name[2:]
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: if iexpr:
check(iexpr, a, a) check(iexpr, a, a)
check(iexpr, a, N1) check(iexpr, a, N1)