Skip to content
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

types.SimpleNamespace should preserve attribute ordering (?) #83256

Closed
ericsnowcurrently opened this issue Dec 17, 2019 · 9 comments
Closed

types.SimpleNamespace should preserve attribute ordering (?) #83256

ericsnowcurrently opened this issue Dec 17, 2019 · 9 comments
Labels
3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@ericsnowcurrently
Copy link
Member

BPO 39075
Nosy @rhettinger, @ericsnowcurrently, @serhiy-storchaka, @1st1, @ZackerySpytz, @miss-islington, @tirkarthi, @shihai1991
PRs
  • bpo-39075: types.SimpleNamespace no longer sorts attributes in its repr #19430
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-05-16.01:30:01.391>
    created_at = <Date 2019-12-17.16:21:21.111>
    labels = ['interpreter-core', 'type-feature', '3.9']
    title = 'types.SimpleNamespace should preserve attribute ordering (?)'
    updated_at = <Date 2020-05-16.01:30:01.390>
    user = 'https://github.com/ericsnowcurrently'

    bugs.python.org fields:

    activity = <Date 2020-05-16.01:30:01.390>
    actor = 'eric.snow'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-05-16.01:30:01.391>
    closer = 'eric.snow'
    components = ['Interpreter Core']
    creation = <Date 2019-12-17.16:21:21.111>
    creator = 'eric.snow'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 39075
    keywords = ['patch']
    message_count = 9.0
    messages = ['358553', '358563', '358613', '358785', '358786', '358793', '358913', '359119', '368995']
    nosy_count = 8.0
    nosy_names = ['rhettinger', 'eric.snow', 'serhiy.storchaka', 'yselivanov', 'ZackerySpytz', 'miss-islington', 'xtreak', 'shihai1991']
    pr_nums = ['19430']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue39075'
    versions = ['Python 3.9']

    @ericsnowcurrently
    Copy link
    Member Author

    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.

    @ericsnowcurrently ericsnowcurrently added 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Dec 17, 2019
    @serhiy-storchaka
    Copy link
    Member

    We usually do not iterate all attributes, and dir() always sort attribute names.

    @rhettinger
    Copy link
    Contributor

    +1 for dropping the sort. Also, the repr should be made to round-trip with eval().

    @shihai1991
    Copy link
    Member

    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?

    @rhettinger
    Copy link
    Contributor

    @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 :-)

    @shihai1991
    Copy link
    Member

    @Raymond OK, copy that.

    @ericsnowcurrently
    Copy link
    Member Author

    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.

    @shihai1991
    Copy link
    Member

    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

    @miss-islington
    Copy link
    Contributor

    New changeset 6b6092f by Zackery Spytz in branch 'master':
    bpo-39075: types.SimpleNamespace no longer sorts attributes in its repr (GH-19430)
    6b6092f

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants