[issue39058] argparse should preserve argument ordering in Namespace

2020-05-17 Thread Raymond Hettinger


Raymond Hettinger  added the comment:


New changeset 9681953c99b686cf23d1c476a2b26d2ddbec7694 by Raymond Hettinger in 
branch 'master':
bpo-39058:  Preserve attribute order in argparse Namespace reprs. (GH-17621)
https://github.com/python/cpython/commit/9681953c99b686cf23d1c476a2b26d2ddbec7694


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39058] argparse should preserve argument ordering in Namespace

2020-05-17 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39058] argparse should preserve argument ordering in Namespace

2019-12-17 Thread paul j3


paul j3  added the comment:

This patch changes the super class, _AttributeHolder.  ArgumentParser and 
Actions also inherit from this, though they have their own _get_kwargs methods, 
and so aren't affected by the sort and its removal.

I just had occasion on stackoverflow to discuss the order in which attributes 
are added.  A poster wanted to preserve the sys.argv order.

https://stackoverflow.com/questions/58904423/find-the-order-of-arguments-in-argparse-python3/58905067#58905067

Most attributes are added as defaults at the start of parsing - via a loop 
through parser._actions.  Predefining the namespace, SUPPRESS defaults, 
parser.set_defaults may alter this default order.

Anyways removing the sort makes sense, and the proposed change phrase "in the 
order attributes were added" is sufficiently general.

--
nosy: +paul.j3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39058] argparse should preserve argument ordering in Namespace

2019-12-17 Thread Eric Snow


Eric Snow  added the comment:

FWIW, I've also opened issue #39076 about subclassing types.SimpleNamespace.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39058] argparse should preserve argument ordering in Namespace

2019-12-17 Thread Eric Snow


Eric Snow  added the comment:

> Currently, Namespace() objects sort the attributes in the __repr__.  This is 
> annoying because argument
> order matters and because everywhere else in the module we preserve order 
> (i.e. users see help in the
> order that arguments are added).

Hmm, I was going to suggest switching to types.SimpleNamespace, but
realized we sort that repr too (likely for the same reason).  I've
opened issue #39075 to address that.

In writing up that issue, I considered that a sorted repr can be
useful in some cases.  However, I don't think any of those cases
really apply here.

Anyway, I agree with your conclusion. :)

--
nosy: +eric.snow

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39058] argparse should preserve argument ordering in Namespace

2019-12-15 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
keywords: +patch
pull_requests: +17091
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/17621

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39058] argparse should preserve argument ordering in Namespace

2019-12-15 Thread Raymond Hettinger


New submission from Raymond Hettinger :

Currently, Namespace() objects sort the attributes in the __repr__.  This is 
annoying because argument order matters and because everywhere else in the 
module we preserve order (i.e. users see help in the order that arguments are 
added).

Note, the docs do not promise that Namespace is displayed with a sort.  This is 
likely just an artifact of older dictionaries having arbitrary or randomised 
ordering.


>>> from argparse import ArgumentParser
>>> parser = ArgumentParser()
>>> _ = parser.add_argument('source')
>>> _ = parser.add_argument('destination')

# Order matters to the user inputing the arguments 
# (source must go first and destination must go last
>>> args = parser.parse_args(['input.txt', 'output.txt'])

# Order is preserved internally
>>> vars(args)
{'source': 'input.txt', 'destination': 'output.txt'}

# Despite this, the Namespace() repr alphabetizes the output
>>> args
Namespace(destination='output.txt', source='input.txt')

# Order is preserved in help()
>>> parser.parse_args(['-h'])   
usage: [-h] source destination

positional arguments:
  source
  destination

optional arguments:
  -h, --help   show this help message and exit

--
components: Library (Lib)
messages: 358455
nosy: rhettinger
priority: normal
severity: normal
status: open
title: argparse should preserve argument ordering in Namespace
type: enhancement
versions: Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com