classification
Title: types.SimpleNamespace should preserve attribute ordering (?)
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ZackerySpytz, eric.snow, miss-islington, rhettinger, serhiy.storchaka, shihai1991, xtreak, yselivanov
Priority: normal Keywords: patch

Created on 2019-12-17 16:21 by eric.snow, last changed 2020-05-16 01:30 by eric.snow. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 19430 merged ZackerySpytz, 2020-04-08 05:45
Messages (9)
msg358553 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2019-12-17 16:21
types.SimpleNamespace was added in 3.3 (for use in sys.implementation; see PEP 421), which predates the change to preserving insertion order in dict.  At the time we chose to sort the attributes in the repr, both for ease of reading and for a consistent output.

The question is, should SimpleNamespace stay as it is (sorted repr) or should it show the order in which attributes were added?

On the one hand, alphabetical order can be useful since it makes it easier for readers to find attributes, especially when there are many.  However, for other cases it is helpful for the repr to show the order in which attributes were added.

FWIW, I favor changing the ordering in the repr to insertion-order.  Either is relatively trivial to get after the fact (whether "sorted(vars(ns))" or "list(vars(ns))"), so I don't think any folks that benefit from alphabetical order will be seriously impacted.
msg358563 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-12-17 18:10
We usually do not iterate all attributes, and dir() always sort attribute names.
msg358613 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-12-18 06:07
+1 for dropping the sort.  Also, the repr should be made to round-trip with eval().
msg358785 - (view) Author: hai shi (shihai1991) * (Python triager) Date: 2019-12-22 05:08
IMHO, dropping the sort should be a default behavior. If some user need  
this feature, maybe we could supply a param to open the sort function or not?
msg358786 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-12-22 08:13
@haisai SimpleNamespace can't use keyword arguments because those are already used to pass in data.  Also, we want to keep it simple; hence, the name :-)
msg358793 - (view) Author: hai shi (shihai1991) * (Python triager) Date: 2019-12-22 16:04
@Raymond OK, copy that.
msg358913 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2019-12-27 20:15
> IMHO, dropping the sort should be a default behavior. If some user need
> this feature, maybe we could supply a param to open the sort function or not?

Consider opening a separate issue (or start a thread on python-ideas)
about adding a more sophisticated implementation to the stdlib's
"reprlib" module.  If you do so then please draw from the
argparse._AttributeHolder implementation.
msg359119 - (view) Author: hai shi (shihai1991) * (Python triager) Date: 2019-12-31 15:28
>Consider opening a separate issue (or start a thread on python-ideas)
>about adding a more sophisticated implementation to the stdlib's
>"reprlib" module.  If you do so then please draw from the
>argparse._AttributeHolder implementation.

FWIW, i created a new bpo: https://bugs.python.org/issue39173
msg368995 - (view) Author: miss-islington (miss-islington) Date: 2020-05-16 01:28
New changeset 6b6092f533f0e4787b8564c4fad6ec6d1018af0d by Zackery Spytz in branch 'master':
bpo-39075: types.SimpleNamespace no longer sorts attributes in its repr (GH-19430)
https://github.com/python/cpython/commit/6b6092f533f0e4787b8564c4fad6ec6d1018af0d
History
Date User Action Args
2020-05-16 01:30:01eric.snowsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-05-16 01:28:01miss-islingtonsetnosy: + miss-islington
messages: + msg368995
2020-04-08 05:45:46ZackerySpytzsetkeywords: + patch
nosy: + ZackerySpytz

pull_requests: + pull_request18787
stage: needs patch -> patch review
2019-12-31 15:28:00shihai1991setmessages: + msg359119
2019-12-27 20:15:15eric.snowsetmessages: + msg358913
2019-12-22 16:04:56shihai1991setmessages: + msg358793
2019-12-22 08:13:37rhettingersetmessages: + msg358786
2019-12-22 05:08:03shihai1991setnosy: + shihai1991
messages: + msg358785
2019-12-18 06:07:21rhettingersetmessages: + msg358613
2019-12-17 18:10:16serhiy.storchakasetnosy: + rhettinger, yselivanov, serhiy.storchaka
messages: + msg358563
2019-12-17 16:46:26xtreaksetnosy: + xtreak
2019-12-17 16:30:05eric.snowsetstage: needs patch
2019-12-17 16:21:21eric.snowcreate