From fe7551bd072a2660a71617e7645749e647f32961 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 2 Jul 2025 10:48:53 +0300 Subject: [PATCH 1/2] gh-136193: Improve `TypeError` msg when comparing two `SimpleNamespace`s --- Lib/test/test_types.py | 19 +++++++++++++++++++ ...-07-02-10-48-21.gh-issue-136193.xfvras.rst | 2 ++ Objects/namespaceobject.c | 8 ++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-07-02-10-48-21.gh-issue-136193.xfvras.rst diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index fc26e71ffcb67b..5e0be5722aa93d 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -21,6 +21,7 @@ import unittest.mock import weakref import typing +import re c_types = import_fresh_module('types', fresh=['_types']) py_types = import_fresh_module('types', blocked=['_types']) @@ -2009,6 +2010,24 @@ def test_equal(self): self.assertEqual(ns1, ns2) self.assertNotEqual(ns2, types.SimpleNamespace()) + def test_richcompare(self): + ns1 = types.SimpleNamespace(x=1) + ns2 = types.SimpleNamespace(y=2) + + msg = re.escape( + "not supported between instances of " + "'types.SimpleNamespace' and 'types.SimpleNamespace'" + ) + + with self.assertRaisesRegex(TypeError, msg): + ns1 > ns2 + with self.assertRaisesRegex(TypeError, msg): + ns1 >= ns2 + with self.assertRaisesRegex(TypeError, msg): + ns1 < ns2 + with self.assertRaisesRegex(TypeError, msg): + ns1 <= ns2 + def test_nested(self): ns1 = types.SimpleNamespace(a=1, b=2) ns2 = types.SimpleNamespace() diff --git a/Misc/NEWS.d/next/Library/2025-07-02-10-48-21.gh-issue-136193.xfvras.rst b/Misc/NEWS.d/next/Library/2025-07-02-10-48-21.gh-issue-136193.xfvras.rst new file mode 100644 index 00000000000000..801115202d0c95 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-07-02-10-48-21.gh-issue-136193.xfvras.rst @@ -0,0 +1,2 @@ +Improve :exc:`TypeError` error message, when richcomparing two +:class:`types.SimpleNamespace` objects. diff --git a/Objects/namespaceobject.c b/Objects/namespaceobject.c index 0fc2bcea4cb06e..201cb8a7df8da1 100644 --- a/Objects/namespaceobject.c +++ b/Objects/namespaceobject.c @@ -194,10 +194,14 @@ namespace_clear(PyObject *op) static PyObject * namespace_richcompare(PyObject *self, PyObject *other, int op) { - if (PyObject_TypeCheck(self, &_PyNamespace_Type) && - PyObject_TypeCheck(other, &_PyNamespace_Type)) + if ( + (op == Py_EQ || op == Py_NE) && + PyObject_TypeCheck(self, &_PyNamespace_Type) && + PyObject_TypeCheck(other, &_PyNamespace_Type) + ) { return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict, ((_PyNamespaceObject *)other)->ns_dict, op); + } Py_RETURN_NOTIMPLEMENTED; } From e46cf19da4842a454b3f365b06b3e897f37f5e9c Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 2 Jul 2025 12:34:32 +0300 Subject: [PATCH 2/2] Update Lib/test/test_types.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 5e0be5722aa93d..116f55051ddb5a 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -2010,7 +2010,7 @@ def test_equal(self): self.assertEqual(ns1, ns2) self.assertNotEqual(ns2, types.SimpleNamespace()) - def test_richcompare(self): + def test_richcompare_unsupported(self): ns1 = types.SimpleNamespace(x=1) ns2 = types.SimpleNamespace(y=2)