]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-136203: Improve `TypeError` msg when comparing two `MappingProxyType`s (#136204)
authorsobolevn <mail@sobolevn.me>
Wed, 2 Jul 2025 16:05:28 +0000 (19:05 +0300)
committerGitHub <noreply@github.com>
Wed, 2 Jul 2025 16:05:28 +0000 (19:05 +0300)
Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/test/test_types.py
Misc/NEWS.d/next/Core_and_Builtins/2025-07-02-15-18-41.gh-issue-136203.Y934sC.rst [new file with mode: 0644]
Objects/descrobject.c

index 116f55051ddb5a072b8cdc6b34f47bc923e17c5c..fccdcc975e6c43ecd2100922b1aec40ec1fe69d9 100644 (file)
@@ -1376,6 +1376,27 @@ class MappingProxyTests(unittest.TestCase):
         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):
 
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-02-15-18-41.gh-issue-136203.Y934sC.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-02-15-18-41.gh-issue-136203.Y934sC.rst
new file mode 100644 (file)
index 0000000..5a622ba
--- /dev/null
@@ -0,0 +1,2 @@
+Improve :exc:`TypeError` error message, when richcomparing two
+:class:`types.MappingProxyType` objects.
index 10c465b95ac19283ba27428cd76f770f2da2518e..d3d17e92b6d1e8f6d1b8d5e40fa8170027352993 100644 (file)
@@ -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