[issue29670] argparse: does not respect required args pre-populated into namespace

paul j3 report at bugs.python.org
Fri Mar 3 23:52:45 EST 2017


paul j3 added the comment:

What issue might have changed this behavior?  I'm not aware of any that were trying to change either the `seen_actions` and the 'required' tests, not any dealing with pre-existing values in Namespace.

The current handling of defaults complicates respecting pre-existing values.  Defaults are added to the namespace at the start of parsing (provided they aren't already present - that respects pre-existing values).  At the end of parsing, the same block that tests for required actions also tests if the defaults in the Namespace need to be converted with the `type` function.

So the code at the end of _parse_known_args has limited ability to distinguish between values in the Namespace that  were preloaded, were loaded as defaults, or were loaded during parsing.

I agree that _parse_known_args is complicated and difficult to customize.  But it's been like that from the start.  And the handling of defaults is also complicated (and made worse by the 'delayed evaluation' change some years ago).

For another bug/issue I'd like to make seen_actions (or seen_non_default_actions) available to the user for testing.  But that isn't easy in a backward compatible fashion.

I think the best choice is to pre-load non-required Actions only.  Preloading should be seen as an alternative way of setting defaults, not a way of getting around the 'required' test.  And if that isn't enough, do some of your own 'required' tests after parsing.

----------
nosy: +paul.j3

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


More information about the Python-bugs-list mailing list