[issue24338] In argparse adding wrong arguments makes malformed namespace

Matthias Bussonnier report at bugs.python.org
Mon Jun 1 01:43:43 CEST 2015


Matthias Bussonnier added the comment:

Minimal changes to the repr seem to work.
I can submit a proper patch.

class N2(Namespace):
    
    def __repr__(self):
        type_name = type(self).__name__
        arg_strings = []
        unarg={}
        for arg in self._get_args():
            arg_strings.append(repr(arg))
        for name, value in self._get_kwargs():
            if name.isidentifier():
                arg_strings.append('%s=%r' % (name, value))
            else:
                unarg[name] = value
        if unarg:
            r_unarg = ', **%s' %(repr(unarg))
        else:
            r_unarg = ''
        return '%s(%s%s)' % (type_name, ', '.join(arg_strings), r_unarg)

>>> N2(a=1, b=2, **{"single ' quote ":"'", 'double " quote':'"'})
N = N2(a=1, b=2, **{"single ' quote ":"'", 'double " quote':'"'})

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24338>
_______________________________________


More information about the Python-bugs-list mailing list