[issue15271] argparse: repeatedly specifying the same argument ignores the previous ones

Ionuț Arțăriși report at bugs.python.org
Sat Jul 7 16:58:48 CEST 2012


Ionuț Arțăriși <ionut at artarisi.eu> added the comment:

So I was looking into this and it seems that there are (at least) two contradicting test cases. When inheriting a parser from two parents, there are two different behaviours for positionals and for options.

In the case of positionals, there is this test:

def test_same_argument_name_parents(self):
        parents = [self.wxyz_parent, self.z_parent]
        parser = ErrorRaisingArgumentParser(parents=parents)
        self.assertEqual(parser.parse_args('1 2'.split()),
                         NS(w=None, y=None, z='2'))

and this is the context from higher up:

self.wxyz_parent.add_argument('z')
...
self.z_parent.add_argument('z')

So the tests don't expect an error when two parents provide the same argument. Instead, the eating up of the first argument (in this case '1') seems to be condoned?


When I tried to change the conflict_handler during parent action merging to 'resolve' I got another test failing:

def test_conflicting_parents(self):
        self.assertRaises(
            argparse.ArgumentError,
            argparse.ArgumentParser,
            parents=[self.w_parent, self.wxyz_parent])

context:

self.wxyz_parent.add_argument('--w')
...
self.w_parent.add_argument('--w')

This tests that two parents which provide the exact same option will raise an error.


So I think the first test is wrong and should be modified to expect an error in the case where two arguments with the same name are provided. Or is there some use case where this behaviour makes sense?

----------

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


More information about the Python-bugs-list mailing list