gh-136203: Improve TypeError msg when comparing two MappingProxyTypes (#136204)
Some checks are pending
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / (push) Blocked by required conditions
Tests / Undefined behavior sanitizer (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
sobolevn 2025-07-02 19:05:28 +03:00 committed by GitHub
parent fa43a1e0f8
commit 41a9b46ad4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 1 deletions

View 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):

View file

@ -0,0 +1,2 @@
Improve :exc:`TypeError` error message, when richcomparing two
:class:`types.MappingProxyType` objects.

View 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