Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.assertEqual(mp1, mp1_2)
self.assertFalse(mp1 != mp1_2)
self.assertFalse(mp1 == mp2)
self.assertNotEqual(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