-
-
Notifications
You must be signed in to change notification settings - Fork 32k
argparse incorrectly says arguments are required #103219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You'll need to show your code, otherwise we can't tell if this is a bug in argparse or your code. Please create a minimal reproducible example: https://stackoverflow.com/help/minimal-reproducible-example |
Added a minimal reproducible example. |
The patch can look something like this: if (action.required and
action.default is None and
action.nargs not in (ZERO_OR_MORE, OPTIONAL)):
required_actions.append(_get_action_name(action)) On line Line 2127 in 24facd6
Test case: def test_required_args(self): # gh-103219
parser = ErrorRaisingArgumentParser(prog='example')
parser.add_argument('foo_arg')
parser.add_argument('baz_arg', default='baz')
parser.add_argument('bar_arg', nargs='*')
args = parser.parse_args(['a'])
self.assertEqual(
NS(foo_arg='a', baz_arg='baz', bar_arg=[]), args)
args = parser.parse_args(['a', 'b', 'c', 'd'])
self.assertEqual(
NS(foo_arg='a', baz_arg='b', bar_arg=['c', 'd']), args)
with self.assertRaises(ArgumentParserError) as cm:
parser.parse_args([])
self.assertRegex(
cm.exception.stderr,
'error: the following arguments are required: foo_arg\n$'
) But, I don't feel confident submitting |
I don't think this needs to be fixed. It's an attempt to correct minor issue in the error message. Normally '*' and '?' positionals are 'seen', because an empty list of arguments meets their In this case required 'foo' positional, blocks all further processing of positionals, including 'bar'. 'bar' can't be filled, with default or not, until 'foo' has been filled. |
Why don't we need to fix an error message? |
This is a duplicate of #72795. |
Since it's a duplicate, I'm closing this. |
When using
nargs='*'
with other required arguments, argparse incorrectly says the optional argument is required.Output (with no arguments)
Code
Python 3.9.16
Linked PRs
The text was updated successfully, but these errors were encountered: