This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author bethard
Recipients bethard
Date 2010-07-23.14:06:50
SpamBayes Score 0.002998486
Marked as misclassified No
Message-id <>
If you use set_defaults on a subparser, but a default exists on the top level parser, the subparser defaults are ignored:

>>> parser = argparse.ArgumentParser()
>>> xparser = parser.add_subparsers().add_parser('X')
>>> parser.set_defaults(foo=1)
>>> xparser.set_defaults(foo=2)
>>> parser.parse_args(['X'])

This is counter to what people probably expect, that the subparser, when selected, would override the top level parser.

The behavior is because of the following code in parse_known_args:

        for dest in self._defaults:
            if not hasattr(namespace, dest):
                setattr(namespace, dest, self._defaults[dest])

This happens before the subparser sees the namespace object, and so the subparser sees that no defaults need to be filled in.
Date User Action Args
2010-07-23 14:06:52bethardsetrecipients: + bethard
2010-07-23 14:06:52bethardsetmessageid: <>
2010-07-23 14:06:50bethardlinkissue9351 messages
2010-07-23 14:06:50bethardcreate