Skip to content

gh-136203: Improve TypeError msg when comparing two MappingProxyTypes #136204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,27 @@ def __hash__(self):
view = self.mappingproxy(mapping)
self.assertEqual(hash(view), hash(mapping))

def test_richcompare(self):
mp1 = self.mappingproxy({'a': 1})
mp1_2 = self.mappingproxy({'a': 1})
mp2 = self.mappingproxy({'a': 2})

self.assertTrue(mp1 == mp1_2)
self.assertFalse(mp1 != mp1_2)
self.assertFalse(mp1 == mp2)
self.assertTrue(mp1 != mp2)

msg = "not supported between instances of 'mappingproxy' and 'mappingproxy'"

with self.assertRaisesRegex(TypeError, msg):
mp1 > mp2
with self.assertRaisesRegex(TypeError, msg):
mp1 < mp1_2
with self.assertRaisesRegex(TypeError, msg):
mp2 >= mp2
with self.assertRaisesRegex(TypeError, msg):
mp1_2 <= mp1


class ClassCreationTests(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve :exc:`TypeError` error message, when richcomparing two
:class:`types.MappingProxyType` objects.
5 changes: 4 additions & 1 deletion Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,10 @@ static PyObject *
mappingproxy_richcompare(PyObject *self, PyObject *w, int op)
{
mappingproxyobject *v = (mappingproxyobject *)self;
return PyObject_RichCompare(v->mapping, w, op);
if (op == Py_EQ || op == Py_NE) {
return PyObject_RichCompare(v->mapping, w, op);
}
Py_RETURN_NOTIMPLEMENTED;
}

static int
Expand Down
Loading