You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
argparse: successive parsing wipes out nargs=? values #72920
I'm writing a wrapper that optionally accepts a file and reads more options from that file. The wrapper then needs to pass all of these options and the file to another program (qsub). Here is a minimal example to reproduce the behavior I'm seeing:
The behavior I expect is that the file should remain as 'myFile', but it is being wiped out. Is there any way to prevent this, or is this actually a bug?
I can recreate this problem in Python 2.7 and 3.5.
The problem described here is restricted to ? and `*' positionals, and is caused by the subtle way in which 'empty' optional positionals are handled.
The regular handling of defaults at the start of parse_known_args works fine. The default is only written to the namespace if something isn't there already.
But a positional with ? or * is always 'seen' because an empty list of strings satisfies the nargs pattern. 'get_values()' has special handling for this case:
That is, it replaces the empty list with the 'default'.
But take_action(), which does the actual saving, is conditional:
# take the action if we didn't receive a SUPPRESS value# (e.g. from a default)ifargument_valuesisnotSUPPRESS:
action(self, namespace, argument_values, option_string)
That explains why 'default=SUPPRESS' solves this issue.
It's enough to satisfy the OP's situation, but I don't think it's a robust fix.
I don't have a patch idea yet, but this probably should be reopened so there's a record of the potential problem.
More on the complications raised by these 'seen default actions' in
Activity
adamjstewart commentedon Nov 18, 2016
I'm writing a wrapper that optionally accepts a file and reads more options from that file. The wrapper then needs to pass all of these options and the file to another program (qsub). Here is a minimal example to reproduce the behavior I'm seeing:
The behavior I expect is that the file should remain as 'myFile', but it is being wiped out. Is there any way to prevent this, or is this actually a bug?
I can recreate this problem in Python 2.7 and 3.5.
wm75 commentedon Nov 18, 2016
try this:
parser.add_argument('file', nargs='?', default = argparse.SUPPRESS)
I don't think this is a bug. The default for the default parameter is None so the behavior is consistent with the documentation.
adamjstewart commentedon Nov 18, 2016
Works for me, thanks Wolfgang!
paulj3 commentedon Jul 27, 2017
The problem described here is restricted to
?and `*' positionals, and is caused by the subtle way in which 'empty' optional positionals are handled.The regular handling of defaults at the start of
parse_known_argsworks fine. The default is only written to the namespace if something isn't there already.But a positional with ? or * is always 'seen' because an empty list of strings satisfies the nargs pattern. 'get_values()' has special handling for this case:
That is, it replaces the empty list with the 'default'.
But take_action(), which does the actual saving, is conditional:
That explains why 'default=SUPPRESS' solves this issue.
It's enough to satisfy the OP's situation, but I don't think it's a robust fix.
I don't have a patch idea yet, but this probably should be reopened so there's a record of the potential problem.
More on the complications raised by these 'seen default actions' in
http://bugs.python.org/issue18943
serhiy-storchaka commentedon Sep 25, 2024
Closing this issue, because the reported case is unusual, the OP is satisfied with the workeraund, and changing the current behavior is really hard.