]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39058: Preserve attribute order in argparse Namespace reprs. (GH-17621)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Mon, 18 May 2020 01:53:01 +0000 (18:53 -0700)
committerGitHub <noreply@github.com>
Mon, 18 May 2020 01:53:01 +0000 (18:53 -0700)
Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS.d/next/Library/2019-12-15-19-17-10.bpo-39058.7ci-vd.rst [new file with mode: 0644]

index 9c710cef5b6aaa5ac3d2e9d42920c44462019ade..2677ef63e9e541f7160b7040655758e2faad3e7c 100644 (file)
@@ -129,7 +129,7 @@ class _AttributeHolder(object):
         return '%s(%s)' % (type_name, ', '.join(arg_strings))
 
     def _get_kwargs(self):
-        return sorted(self.__dict__.items())
+        return list(self.__dict__.items())
 
     def _get_args(self):
         return []
index 9899a53d6d197424ee1db7867900549f6cb98eab..e82a0c39c21a8b0c8dc44a1d1437ed26ef578efd 100644 (file)
@@ -4725,7 +4725,7 @@ class TestStrings(TestCase):
 
     def test_namespace(self):
         ns = argparse.Namespace(foo=42, bar='spam')
-        string = "Namespace(bar='spam', foo=42)"
+        string = "Namespace(foo=42, bar='spam')"
         self.assertStringEqual(ns, string)
 
     def test_namespace_starkwargs_notidentifier(self):
diff --git a/Misc/NEWS.d/next/Library/2019-12-15-19-17-10.bpo-39058.7ci-vd.rst b/Misc/NEWS.d/next/Library/2019-12-15-19-17-10.bpo-39058.7ci-vd.rst
new file mode 100644 (file)
index 0000000..fff1322
--- /dev/null
@@ -0,0 +1,4 @@
+In the argparse module, the repr for Namespace() and other argument holders
+now displayed in the order attributes were added.  Formerly, it displayed in
+alphabetical order even though argument order is preserved the user visible
+parts of the module.