Skip to content

Commit 9681953

Browse files
authored
bpo-39058: Preserve attribute order in argparse Namespace reprs. (GH-17621)
1 parent eefd4e0 commit 9681953

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def __repr__(self):
129129
return '%s(%s)' % (type_name, ', '.join(arg_strings))
130130

131131
def _get_kwargs(self):
132-
return sorted(self.__dict__.items())
132+
return list(self.__dict__.items())
133133

134134
def _get_args(self):
135135
return []

Lib/test/test_argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4725,7 +4725,7 @@ def test_argument(self):
47254725

47264726
def test_namespace(self):
47274727
ns = argparse.Namespace(foo=42, bar='spam')
4728-
string = "Namespace(bar='spam', foo=42)"
4728+
string = "Namespace(foo=42, bar='spam')"
47294729
self.assertStringEqual(ns, string)
47304730

47314731
def test_namespace_starkwargs_notidentifier(self):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
In the argparse module, the repr for Namespace() and other argument holders
2+
now displayed in the order attributes were added. Formerly, it displayed in
3+
alphabetical order even though argument order is preserved the user visible
4+
parts of the module.

0 commit comments

Comments
 (0)