[New-bugs-announce] [issue24360] improve argparse.Namespace __repr__ for invalid identifiers.

Matthias Bussonnier report at bugs.python.org
Tue Jun 2 08:00:51 CEST 2015

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 <report at bugs.python.org>

More information about the New-bugs-announce mailing list