Mailing List Archive

[3.12] gh-117797: Improve `test_descr.test_not_implemented` (GH-117798) (#117921)
https://github.com/python/cpython/commit/44eab29cbd5fd9c120919d1c3b647d2241e055ba
commit: 44eab29cbd5fd9c120919d1c3b647d2241e055ba
branch: 3.12
author: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
committer: sobolevn <mail@sobolevn.me>
date: 2024-04-16T12:50:10+03:00
summary:

[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 1a1e013a4a526546c373afd887f2e25eecc984ad)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>

files:
M Lib/test/test_descr.py

diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index a969f04b10a865..3a11435e3e2543 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
@@ -4626,12 +4624,23 @@ def check(expr, x, y):
('__and__', 'x & y', 'x &= y'),
('__or__', 'x | y', 'x |= y'),
('__xor__', 'x ^ y', 'x ^= y')]:
- rname = '__r' + name[2:]
+ # Defines '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)
+ 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)

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-leave@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: list-python-checkins@lists.gossamer-threads.com