Skip to content

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

Closed
eslindsey opened this issue Apr 3, 2023 · 7 comments
Closed

argparse incorrectly says arguments are required #103219

eslindsey opened this issue Apr 3, 2023 · 7 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@eslindsey
Copy link

eslindsey commented Apr 3, 2023

When using nargs='*' with other required arguments, argparse incorrectly says the optional argument is required.

Output (with no arguments)

usage: example [-h] foo [bar ...]
example: error: the following arguments are required: foo, bar

Code

import argparse

parser = argparse.ArgumentParser(prog='example')
parser.add_argument('foo')
parser.add_argument('bar', nargs='*')
args = parser.parse_args()

Python 3.9.16

Linked PRs

@ericvsmith
Copy link
Member

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

@eslindsey
Copy link
Author

eslindsey commented Apr 3, 2023

Added a minimal reproducible example.

@sobolevn
Copy link
Member

sobolevn commented Apr 3, 2023

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

if action.required:

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 argparse patches. I hope this will help someone else :)

@arhadthedev arhadthedev added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels Apr 4, 2023
@hpaulj
Copy link

hpaulj commented Apr 18, 2023

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 nargs requirments.

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.

@sobolevn
Copy link
Member

I don't think this needs to be fixed. It's an attempt to correct minor issue in the error message.

Why don't we need to fix an error message?

@serhiy-storchaka
Copy link
Member

This is a duplicate of #72795.

@ericvsmith
Copy link
Member

Since it's a duplicate, I'm closing this.

@ericvsmith ericvsmith closed this as not planned Won't fix, can't repro, duplicate, stale Sep 23, 2024
@github-project-automation github-project-automation bot moved this from Bugs to Doc issues in Argparse issues Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Status: Doc issues
Development

No branches or pull requests

6 participants