Skip to content

bpo-39058: Preserve attribute order in the repr for argparse.Namespace() #17621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def __repr__(self):
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 []
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4687,7 +4687,7 @@ def test_argument(self):

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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.