mirror of
https://github.com/python/cpython.git
synced 2025-07-07 19:35:27 +00:00
gh-136193: Improve TypeError
msg when comparing two SimpleNamespace
s (#136195)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
parent
51ab66b3d5
commit
ab7196a2f5
3 changed files with 27 additions and 2 deletions
|
@ -21,6 +21,7 @@ import types
|
||||||
import unittest.mock
|
import unittest.mock
|
||||||
import weakref
|
import weakref
|
||||||
import typing
|
import typing
|
||||||
|
import re
|
||||||
|
|
||||||
c_types = import_fresh_module('types', fresh=['_types'])
|
c_types = import_fresh_module('types', fresh=['_types'])
|
||||||
py_types = import_fresh_module('types', blocked=['_types'])
|
py_types = import_fresh_module('types', blocked=['_types'])
|
||||||
|
@ -2009,6 +2010,24 @@ class SimpleNamespaceTests(unittest.TestCase):
|
||||||
self.assertEqual(ns1, ns2)
|
self.assertEqual(ns1, ns2)
|
||||||
self.assertNotEqual(ns2, types.SimpleNamespace())
|
self.assertNotEqual(ns2, types.SimpleNamespace())
|
||||||
|
|
||||||
|
def test_richcompare_unsupported(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):
|
def test_nested(self):
|
||||||
ns1 = types.SimpleNamespace(a=1, b=2)
|
ns1 = types.SimpleNamespace(a=1, b=2)
|
||||||
ns2 = types.SimpleNamespace()
|
ns2 = types.SimpleNamespace()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Improve :exc:`TypeError` error message, when richcomparing two
|
||||||
|
:class:`types.SimpleNamespace` objects.
|
|
@ -194,10 +194,14 @@ namespace_clear(PyObject *op)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
namespace_richcompare(PyObject *self, PyObject *other, int op)
|
namespace_richcompare(PyObject *self, PyObject *other, int op)
|
||||||
{
|
{
|
||||||
if (PyObject_TypeCheck(self, &_PyNamespace_Type) &&
|
if (
|
||||||
PyObject_TypeCheck(other, &_PyNamespace_Type))
|
(op == Py_EQ || op == Py_NE) &&
|
||||||
|
PyObject_TypeCheck(self, &_PyNamespace_Type) &&
|
||||||
|
PyObject_TypeCheck(other, &_PyNamespace_Type)
|
||||||
|
) {
|
||||||
return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict,
|
return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict,
|
||||||
((_PyNamespaceObject *)other)->ns_dict, op);
|
((_PyNamespaceObject *)other)->ns_dict, op);
|
||||||
|
}
|
||||||
Py_RETURN_NOTIMPLEMENTED;
|
Py_RETURN_NOTIMPLEMENTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue