gh-136203: Improve `TypeError` msg when comparing two `MappingProxyTy… · python/cpython@41a9b46 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit 41a9b46

Browse files
sobolevnvstinner
andauthored
gh-136203: Improve TypeError msg when comparing two MappingProxyTypes (#136204)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent fa43a1e commit 41a9b46

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Lib/test/test_types.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,27 @@ def __hash__(self):
13761376
view = self.mappingproxy(mapping)
13771377
self.assertEqual(hash(view), hash(mapping))
13781378

1379+
def test_richcompare(self):
1380+
mp1 = self.mappingproxy({'a': 1})
1381+
mp1_2 = self.mappingproxy({'a': 1})
1382+
mp2 = self.mappingproxy({'a': 2})
1383+
1384+
self.assertTrue(mp1 == mp1_2)
1385+
self.assertFalse(mp1 != mp1_2)
1386+
self.assertFalse(mp1 == mp2)
1387+
self.assertTrue(mp1 != mp2)
1388+
1389+
msg = "not supported between instances of 'mappingproxy' and 'mappingproxy'"
1390+
1391+
with self.assertRaisesRegex(TypeError, msg):
1392+
mp1 > mp2
1393+
with self.assertRaisesRegex(TypeError, msg):
1394+
mp1 < mp1_2
1395+
with self.assertRaisesRegex(TypeError, msg):
1396+
mp2 >= mp2
1397+
with self.assertRaisesRegex(TypeError, msg):
1398+
mp1_2 <= mp1
1399+
13791400

13801401
class ClassCreationTests(unittest.TestCase):
13811402

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve :exc:`TypeError` error message, when richcomparing two
2+
:class:`types.MappingProxyType` objects.

Objects/descrobject.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,10 @@ static PyObject *
12331233
mappingproxy_richcompare(PyObject *self, PyObject *w, int op)
12341234
{
12351235
mappingproxyobject *v = (mappingproxyobject *)self;
1236-
return PyObject_RichCompare(v->mapping, w, op);
1236+
if (op == Py_EQ || op == Py_NE) {
1237+
return PyObject_RichCompare(v->mapping, w, op);
1238+
}
1239+
Py_RETURN_NOTIMPLEMENTED;
12371240
}
12381241

12391242
static int

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.