From 0a1d2e7b5919a2a35867331a88797f95fbb1014b Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Tue, 24 Nov 2020 13:56:59 -0700 Subject: [PATCH] bpo-42088: Use "SimpleNamespace(...)" in the repr for SimpleNamespace The repr is now "SimpleNamespace(...)" instead of "namespace(...)". --- Lib/test/test_pprint.py | 4 ++-- Lib/test/test_types.py | 4 ++-- .../next/Library/2020-11-24-13-55-52.bpo-42088.tjTf-0.rst | 2 ++ Objects/namespaceobject.c | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-11-24-13-55-52.bpo-42088.tjTf-0.rst diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index c4a8578a9fc..e45c3aa13c8 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -396,12 +396,12 @@ mappingproxy(OrderedDict([('the', 0), def test_empty_simple_namespace(self): ns = types.SimpleNamespace() formatted = pprint.pformat(ns) - self.assertEqual(formatted, "namespace()") + self.assertEqual(formatted, "SimpleNamespace()") def test_small_simple_namespace(self): ns = types.SimpleNamespace(a=1, b=2) formatted = pprint.pformat(ns) - self.assertEqual(formatted, "namespace(a=1, b=2)") + self.assertEqual(formatted, "SimpleNamespace(a=1, b=2)") def test_simple_namespace(self): ns = types.SimpleNamespace( diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 3058a02d6ee..cf56bc8b394 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -1406,7 +1406,7 @@ class SimpleNamespaceTests(unittest.TestCase): ns2 = types.SimpleNamespace() ns2.x = "spam" ns2._y = 5 - name = "namespace" + name = "SimpleNamespace" self.assertEqual(repr(ns1), "{name}(x=1, y=2, w=3)".format(name=name)) self.assertEqual(repr(ns2), "{name}(x='spam', _y=5)".format(name=name)) @@ -1456,7 +1456,7 @@ class SimpleNamespaceTests(unittest.TestCase): ns1.spam = ns1 ns2.spam = ns3 ns3.spam = ns2 - name = "namespace" + name = "SimpleNamespace" repr1 = "{name}(c='cookie', spam={name}(...))".format(name=name) repr2 = "{name}(spam={name}(x=1, spam={name}(...)))".format(name=name) diff --git a/Misc/NEWS.d/next/Library/2020-11-24-13-55-52.bpo-42088.tjTf-0.rst b/Misc/NEWS.d/next/Library/2020-11-24-13-55-52.bpo-42088.tjTf-0.rst new file mode 100644 index 00000000000..9208beed562 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-11-24-13-55-52.bpo-42088.tjTf-0.rst @@ -0,0 +1,2 @@ +The repr of :class:`types.SimpleNamespace` is now ``SimpleNamespace(...)`` +instead of ``namespace(...)``. diff --git a/Objects/namespaceobject.c b/Objects/namespaceobject.c index fa37ed250d3..1e833e08d41 100644 --- a/Objects/namespaceobject.c +++ b/Objects/namespaceobject.c @@ -72,7 +72,7 @@ namespace_repr(PyObject *ns) PyObject *separator, *pairsrepr, *repr = NULL; const char * name; - name = Py_IS_TYPE(ns, &_PyNamespace_Type) ? "namespace" + name = Py_IS_TYPE(ns, &_PyNamespace_Type) ? "SimpleNamespace" : Py_TYPE(ns)->tp_name; i = Py_ReprEnter(ns);