bpo-39075: types.SimpleNamespace no longer sorts attributes in its repr (GH-19430)

This commit is contained in:
Zackery Spytz 2020-05-15 19:27:54 -06:00 committed by GitHub
parent 1ce5841eca
commit 6b6092f533
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 7 deletions

View File

@ -355,8 +355,7 @@ Additional Utility Classes and Functions
self.__dict__.update(kwargs) self.__dict__.update(kwargs)
def __repr__(self): def __repr__(self):
keys = sorted(self.__dict__) items = (f"{k}={v!r}" for k, v in self.__dict__.items())
items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
return "{}({})".format(type(self).__name__, ", ".join(items)) return "{}({})".format(type(self).__name__, ", ".join(items))
def __eq__(self, other): def __eq__(self, other):
@ -368,6 +367,9 @@ Additional Utility Classes and Functions
.. versionadded:: 3.3 .. 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) .. function:: DynamicClassAttribute(fget=None, fset=None, fdel=None, doc=None)

View File

@ -1262,8 +1262,8 @@ class SimpleNamespaceTests(unittest.TestCase):
ns2._y = 5 ns2._y = 5
name = "namespace" name = "namespace"
self.assertEqual(repr(ns1), "{name}(w=3, x=1, y=2)".format(name=name)) self.assertEqual(repr(ns1), "{name}(x=1, y=2, w=3)".format(name=name))
self.assertEqual(repr(ns2), "{name}(_y=5, x='spam')".format(name=name)) self.assertEqual(repr(ns2), "{name}(x='spam', _y=5)".format(name=name))
def test_equal(self): def test_equal(self):
ns1 = types.SimpleNamespace(x=1) ns1 = types.SimpleNamespace(x=1)
@ -1312,7 +1312,7 @@ class SimpleNamespaceTests(unittest.TestCase):
ns3.spam = ns2 ns3.spam = ns2
name = "namespace" name = "namespace"
repr1 = "{name}(c='cookie', spam={name}(...))".format(name=name) 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(ns1), repr1)
self.assertEqual(repr(ns2), repr2) self.assertEqual(repr(ns2), repr2)

View File

@ -0,0 +1,2 @@
The repr for :class:`types.SimpleNamespace` is now insertion ordered rather
than alphabetical.

View File

@ -91,8 +91,6 @@ namespace_repr(PyObject *ns)
keys = PyDict_Keys(d); keys = PyDict_Keys(d);
if (keys == NULL) if (keys == NULL)
goto error; goto error;
if (PyList_Sort(keys) != 0)
goto error;
keys_iter = PyObject_GetIter(keys); keys_iter = PyObject_GetIter(keys);
if (keys_iter == NULL) if (keys_iter == NULL)