gh-136193: Improve `TypeError` msg when comparing two `SimpleNamespac… · python/cpython@ab7196a · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit ab7196a

Browse files
sobolevnpicnixz
andauthored
gh-136193: Improve TypeError msg when comparing two SimpleNamespaces (#136195)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 51ab66b commit ab7196a

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

Lib/test/test_types.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import unittest.mock
2222
import weakref
2323
import typing
24+
import re
2425

2526
c_types = import_fresh_module('types', fresh=['_types'])
2627
py_types = import_fresh_module('types', blocked=['_types'])
@@ -2009,6 +2010,24 @@ def test_equal(self):
20092010
self.assertEqual(ns1, ns2)
20102011
self.assertNotEqual(ns2, types.SimpleNamespace())
20112012

2013+
def test_richcompare_unsupported(self):
2014+
ns1 = types.SimpleNamespace(x=1)
2015+
ns2 = types.SimpleNamespace(y=2)
2016+
2017+
msg = re.escape(
2018+
"not supported between instances of "
2019+
"'types.SimpleNamespace' and 'types.SimpleNamespace'"
2020+
)
2021+
2022+
with self.assertRaisesRegex(TypeError, msg):
2023+
ns1 > ns2
2024+
with self.assertRaisesRegex(TypeError, msg):
2025+
ns1 >= ns2
2026+
with self.assertRaisesRegex(TypeError, msg):
2027+
ns1 < ns2
2028+
with self.assertRaisesRegex(TypeError, msg):
2029+
ns1 <= ns2
2030+
20122031
def test_nested(self):
20132032
ns1 = types.SimpleNamespace(a=1, b=2)
20142033
ns2 = types.SimpleNamespace()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve :exc:`TypeError` error message, when richcomparing two
2+
:class:`types.SimpleNamespace` objects.

Objects/namespaceobject.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,14 @@ namespace_clear(PyObject *op)
194194
static PyObject *
195195
namespace_richcompare(PyObject *self, PyObject *other, int op)
196196
{
197-
if (PyObject_TypeCheck(self, &_PyNamespace_Type) &&
198-
PyObject_TypeCheck(other, &_PyNamespace_Type))
197+
if (
198+
(op == Py_EQ || op == Py_NE) &&
199+
PyObject_TypeCheck(self, &_PyNamespace_Type) &&
200+
PyObject_TypeCheck(other, &_PyNamespace_Type)
201+
) {
199202
return PyObject_RichCompare(((_PyNamespaceObject *)self)->ns_dict,
200203
((_PyNamespaceObject *)other)->ns_dict, op);
204+
}
201205
Py_RETURN_NOTIMPLEMENTED;
202206
}
203207

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.