Skip to content

argparse incorrectly says arguments are required #103219

Closed as not planned
Closed as not planned

Description

@eslindsey

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

Activity

  1. ericvsmith commented on Apr 3, 2023

    @ericvsmith
    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

  2. eslindsey commented on Apr 3, 2023

    @eslindsey
    Author

    Added a minimal reproducible example.

  3. sobolevn commented on Apr 3, 2023

    @sobolevn
    Member

    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 :)

  4. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Apr 4, 2023
  5. hpaulj commented on Apr 18, 2023

    @hpaulj

    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.

  6. sobolevn commented on Apr 19, 2023

    @sobolevn
    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?

  7. serhiy-storchaka commented on Sep 21, 2024

    @serhiy-storchaka
    Member

    This is a duplicate of #72795.

  8. ericvsmith commented on Sep 23, 2024

    @ericvsmith
    Member

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

  9. moved this from Bugs to Doc issues in Argparse issueson Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions