Skip to content

Argparse argument names with _ in name can be parsed incorrectly and take invalid options. #131175

@flyingaceaaa

Description

@flyingaceaaa

Bug description:

When using argparse library and adding arguments, having an argument name such as --my_arg can result in python interpreting any matching --my argument passed to command line to be viewed as --my_arg.

For Example, if I call my file below with the following argument, it will not error even though --model is not a valid argument created in parser, but --model_path is.

<python_version> test_arg_error.py --model /some/arbitrary/path

python file name: test_arg_error.py

import argparse 
parser = argparse.ArgumentParser(description=" Argparse Error Description")
parser.add_argument("--model_path")
args = parser.parse_args()

CPython versions tested on:

3.10, 3.9

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Projects

    Status

    Doc issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions