New submission from Matthias Bussonnier:

The argparse Namespace can be missleading in case where the args names are not 
valid identifiers, eg thinks like a closing bracket:

In [5]: Namespace(a=1, **{')':3})
Out[5]: Namespace()=3, a=1)

more funny:

In [3]: Namespace(a=1, **{s:3})
Out[3]: Namespace(a=1, b=2), Namespace(c=3)

for `s = 'b=2), Namespace(c'`


With this patch the args that are not valid identifiers are shown in ** 
unpacked-dict, which has the side effect of almost always having 
repr(eval(repr(obj)))== repr(obj). I'm sure we can find counter example with 
quotes and triple-quoted string... but anyway.


with this patch (indentation mine for easy comparison):

>>> from argparse import Namespace
>>> Namespace(a=1, **{')': 3})
    Namespace(a=1, **{')': 3})

Which is I think what most user would expect.

Test passes locally (except SSL cert, network thingies, curses and 
threaded_lru_cache) which look unrelated and is most likely due to my machine.

----------
components: Library (Lib)
files: improve-namespace-repr.patch
keywords: patch
messages: 244655
nosy: mbussonn
priority: normal
severity: normal
status: open
title: improve argparse.Namespace __repr__ for invalid identifiers.
type: enhancement
versions: Python 3.6
Added file: http://bugs.python.org/file39593/improve-namespace-repr.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24360>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to