Skip to content

Argparse logs incorrect ambiguous option #125254

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
clavedeluna opened this issue Oct 10, 2024 · 2 comments
Closed

Argparse logs incorrect ambiguous option #125254

clavedeluna opened this issue Oct 10, 2024 · 2 comments
Assignees
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@clavedeluna
Copy link

clavedeluna commented Oct 10, 2024

Bug report

Bug description:

Given this example script

import argparse
class CsvListAction(argparse.Action):
    """
    argparse Action to convert "a,b,c" into ["a", "b", "c"]
    """

    def __call__(self, parser, namespace, values, option_string=None):
        # Conversion to dict removes duplicates while preserving order
        items = list(dict.fromkeys(values.split(",")).keys())
        setattr(namespace, self.dest, items)


parser = argparse.ArgumentParser(description="Testing")

parser.add_argument("directory", type=str, help="path to find files")
parser.add_argument(
    "--output",
    type=str,
    help="name of output file to produce",
)

name_args_group = parser.add_mutually_exclusive_group()
name_args_group.add_argument(
    "--name-exclude",
    action=CsvListAction,
    help="Comma-separated set of name ID(s) to exclude",
)
name_args_group.add_argument(
    "--name-include",
    action=CsvListAction,
    help="Comma-separated set of name ID(s) to include",
)


parser.add_argument(
    "--path-exclude",
    action=CsvListAction,
    default=[],
    help="Comma-separated set of UNIX glob patterns to exclude",
)
parser.add_argument(
    "--path-include",
    action=CsvListAction,
    default=[],
    help="Comma-separated set of UNIX glob patterns to include",
)
parser.parse_args()

Notice the cli args are name-exclude and name-include

Run the following command, which incorrectly puts the arg as --name
python argparse_mini.py /some/path --output here.txt --name something --path-exclude *request.py

If you run it with Python 3.12.2 or below, the resulting log statement is what we expect
argparse_mini.py: error: ambiguous option: --name could match --name-exclude, --name-include

However, tested with 3.12.7 and the log statement instead is
argparse_mini.py: error: ambiguous option: *request.py could match --name-exclude, --name-include

which is clearly incorrect, as *request.py was the argument to a different CLI option

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

macOS

Linked PRs

@clavedeluna clavedeluna added the type-bug An unexpected behavior, bug, or error label Oct 10, 2024
@ZeroIntensity ZeroIntensity added the stdlib Python modules in the Lib dir label Oct 10, 2024
@ZeroIntensity
Copy link
Member

cc @savannahostrowski, you've been working on argparse recently, right?

@serhiy-storchaka serhiy-storchaka self-assigned this Oct 10, 2024
@serhiy-storchaka serhiy-storchaka added 3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes labels Oct 10, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Oct 10, 2024
This was a regression introduced in pythongh-58573. It was only tested for the
case when the ambiguous option is the last argument in the command line.
@serhiy-storchaka
Copy link
Member

Thank you for your report @clavedeluna. This was a regression introduced in #58573.

serhiy-storchaka added a commit that referenced this issue Oct 12, 2024
…5273)

This was a regression introduced in gh-58573. It was only tested for the
case when the ambiguous option is the last argument in the command line.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 12, 2024
…ythonGH-125273)

This was a regression introduced in pythongh-58573. It was only tested for the
case when the ambiguous option is the last argument in the command line.
(cherry picked from commit 63cf4e9)

Co-authored-by: Serhiy Storchaka <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 12, 2024
…ythonGH-125273)

This was a regression introduced in pythongh-58573. It was only tested for the
case when the ambiguous option is the last argument in the command line.
(cherry picked from commit 63cf4e9)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit that referenced this issue Oct 12, 2024
…GH-125273) (GH-125360)

This was a regression introduced in gh-58573. It was only tested for the
case when the ambiguous option is the last argument in the command line.
(cherry picked from commit 63cf4e9)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit that referenced this issue Oct 12, 2024
…GH-125273) (GH-125359)

This was a regression introduced in gh-58573. It was only tested for the
case when the ambiguous option is the last argument in the command line.
(cherry picked from commit 63cf4e9)

Co-authored-by: Serhiy Storchaka <[email protected]>
@github-project-automation github-project-automation bot moved this from Bugs to Doc issues in Argparse issues Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes 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

3 participants