diff options
author | Zackery Spytz <zspytz@gmail.com> | 2020-05-16 01:27:54 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-16 01:27:54 (GMT) |
commit | 6b6092f533f0e4787b8564c4fad6ec6d1018af0d (patch) | |
tree | 7b3986eeb2dd969a78a6ed63daf6df6a0c5af482 | |
parent | 1ce5841eca6d96b1b1e8c213d04f2e92b1619bb5 (diff) | |
download | cpython-6b6092f533f0e4787b8564c4fad6ec6d1018af0d.zip cpython-6b6092f533f0e4787b8564c4fad6ec6d1018af0d.tar.gz cpython-6b6092f533f0e4787b8564c4fad6ec6d1018af0d.tar.bz2 |
bpo-39075: types.SimpleNamespace no longer sorts attributes in its repr (GH-19430)
-rw-r--r-- | Doc/library/types.rst | 6 | ||||
-rw-r--r-- | Lib/test/test_types.py | 6 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-04-07-23-44-06.bpo-39075.hgck3j.rst | 2 | ||||
-rw-r--r-- | Objects/namespaceobject.c | 2 |
4 files changed, 9 insertions, 7 deletions
diff --git a/Doc/library/types.rst b/Doc/library/types.rst index cdddb46..79acdf4 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -355,8 +355,7 @@ Additional Utility Classes and Functions self.__dict__.update(kwargs) def __repr__(self): - keys = sorted(self.__dict__) - items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys) + items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): @@ -368,6 +367,9 @@ Additional Utility Classes and Functions .. versionadded:: 3.3 + .. versionchanged:: 3.9 + Attribute order in the repr changed from alphabetical to insertion (like + ``dict``). .. function:: DynamicClassAttribute(fget=None, fset=None, fdel=None, doc=None) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 28ebfb6..49dc5bf 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -1262,8 +1262,8 @@ class SimpleNamespaceTests(unittest.TestCase): ns2._y = 5 name = "namespace" - self.assertEqual(repr(ns1), "{name}(w=3, x=1, y=2)".format(name=name)) - self.assertEqual(repr(ns2), "{name}(_y=5, x='spam')".format(name=name)) + 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)) def test_equal(self): ns1 = types.SimpleNamespace(x=1) @@ -1312,7 +1312,7 @@ class SimpleNamespaceTests(unittest.TestCase): ns3.spam = ns2 name = "namespace" repr1 = "{name}(c='cookie', spam={name}(...))".format(name=name) - repr2 = "{name}(spam={name}(spam={name}(...), x=1))".format(name=name) + repr2 = "{name}(spam={name}(x=1, spam={name}(...)))".format(name=name) self.assertEqual(repr(ns1), repr1) self.assertEqual(repr(ns2), repr2) diff --git a/Misc/NEWS.d/next/Library/2020-04-07-23-44-06.bpo-39075.hgck3j.rst b/Misc/NEWS.d/next/Library/2020-04-07-23-44-06.bpo-39075.hgck3j.rst new file mode 100644 index 0000000..c447a19 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-04-07-23-44-06.bpo-39075.hgck3j.rst @@ -0,0 +1,2 @@ +The repr for :class:`types.SimpleNamespace` is now insertion ordered rather +than alphabetical. diff --git a/Objects/namespaceobject.c b/Objects/namespaceobject.c index 29141a8..fa37ed2 100644 --- a/Objects/namespaceobject.c +++ b/Objects/namespaceobject.c @@ -91,8 +91,6 @@ namespace_repr(PyObject *ns) keys = PyDict_Keys(d); if (keys == NULL) goto error; - if (PyList_Sort(keys) != 0) - goto error; keys_iter = PyObject_GetIter(keys); if (keys_iter == NULL) |