classification
Title: _AttributeHolder of argparse should support the sort function or not?
Type: enhancement Stage: resolved
Components: Library (Lib) Versions:
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: SilentGhost, paul.j3, rhettinger, shihai1991
Priority: normal Keywords:

Created on 2019-12-31 15:23 by shihai1991, last changed 2020-05-18 01:54 by rhettinger. This issue is now closed.

Messages (9)
msg359118 - (view) Author: hai shi (shihai1991) * Date: 2019-12-31 15:23
Currently, many developers discuss the output of attributes of argparse should be sorted or not?

>>> from argparse import ArgumentParser
>>> parser = ArgumentParser()
>>> _ = parser.add_argument('outstream')
>>> _ = parser.add_argument('instream')
>>> args = parser.parse_args(['out.txt', 'in.txt'])

# Keep the original order
>>> vars(args)

{'outstream': 'out.txt', 'instream': 'in.txt'}
# Order is sorted
>>> args
Namespace(instream='in.txt', outstream='out.txt')

IMHO, the attributes order should be keep the original order by default. If user would like use order the attributes order, we should add a param in `_AttributeHolder` to open sorting or not.

such as:
```
class _AttributeHolder(object):
    def __init__(self, sort=false):
        self.sort = sort

    def _get_kwargs(self):
        if sort:
            return sorted(self.__dict__.items())
        else:
            return self.__dict__.items()
```

some other bpos have discussed this topic too: issue39075issue39058
msg359121 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-12-31 18:25
What is the use-case for this? Why would anyone care in which order arguments are represented in the object repr?
msg359139 - (view) Author: hai shi (shihai1991) * Date: 2020-01-01 01:47
Users or developers interact with program by cli(argparse), so attributes' output order affect users' interaction.
msg359149 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2020-01-01 08:49
You'd need to elaborate on that. I've been using argparse for years and never once had presentational aspects affected by work. So, again, what is the actual use case for this?
msg359189 - (view) Author: hai shi (shihai1991) * Date: 2020-01-02 11:20
1)>You'd need to elaborate on that. I've been using argparse for years and never once had presentational aspects affected by work. So, again, what is the actual use case for this?
  _AttributeHolder_ has sorted the args in `_get_kwargs()` and the offspring class override this function. So you have accepted this default behavior(do not use sorted attributes).
2)some guys have discussed this question in stackoverflow, such as: https://stackoverflow.com/questions/12268602/sort-argparse-help-alphabetically
3)This bpo stay in discussion stage and receive everyone's opinion ;)
msg359203 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2020-01-02 15:52
The answer to the SO question seems rather complete to me and the only thing to be done on our side would be to document Formatter class and perhaps provide example implementation for sorting formatter. Assuming Raymond goes ahead with #39058 I'm still not sure who the proposed additional argument would be interesting to.
msg360233 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2020-01-18 04:56
I don't think the SO question is relevant.  It's about changing the order of Actions in the Help and/or usage.    

Here it's a question of whether to sort the  `argparse.Namespace` display.  I think it's been adequately discussed in the recent bug/issues such as 39058.
msg360237 - (view) Author: hai shi (shihai1991) * Date: 2020-01-18 05:49
paul said in issue 39058: `Anyways removing the sort makes sense, and the proposed change phrase "in the order attributes were added" is sufficiently general.`

I agree paul's opinion now. The functionality meets users' needs now, so we don't need spend much time in it;)
msg369161 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-05-18 01:54
Am marking this as closed, out-of-date.

If needed, feel free to re-open.
History
Date User Action Args
2020-05-18 01:54:34rhettingersetstatus: open -> closed
resolution: out of date
messages: + msg369161

stage: resolved
2020-01-18 05:49:51shihai1991setmessages: + msg360237
2020-01-18 04:56:23paul.j3setnosy: + paul.j3
messages: + msg360233
2020-01-02 15:52:52SilentGhostsetmessages: + msg359203
2020-01-02 11:20:05shihai1991setmessages: + msg359189
2020-01-01 08:49:30SilentGhostsetmessages: + msg359149
2020-01-01 01:47:52shihai1991setmessages: + msg359139
2019-12-31 21:09:33rhettingersetassignee: rhettinger

nosy: + rhettinger
2019-12-31 18:25:39SilentGhostsetnosy: + SilentGhost
messages: + msg359121
2019-12-31 15:23:53shihai1991create