Skip to content
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

ArgumentParser negative number regex too restrictive #105712

Closed
marcusmueller opened this issue Jun 12, 2023 · 2 comments
Closed

ArgumentParser negative number regex too restrictive #105712

marcusmueller opened this issue Jun 12, 2023 · 2 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@marcusmueller
Copy link

marcusmueller commented Jun 12, 2023

Bug report

Background Story

GNU Radio has a python program design tool, the GNU Radio companion. It creates Python programs from visually designed flow graphs.

User expect to be able to define command line parameters. We support that by generating ArgumentParser-based argument parsers.

Since GNU Radio is used a lot with scientific notation, it's very common for users to enter numbers such as 2.4e9 on the command line, or -3e-12.

The latter is what breaks

Problem

parser.add_argument("-p", "--param", type=float)
args = parser.parse_args()

works with

./prog.py -p 3e12
./prog.py -p -42

but doesn't work with

./prog.py -p -3e12

as the latter is misinterpreted, with -3e12 being interpreted as optional argument that's undefined.

Root analysis

argparse.py specifies

cpython/Lib/argparse.py

Lines 1402 to 1403 in 8da9d1b

# determines whether an "option" looks like a negative number
self._negative_number_matcher = _re.compile(r'^-\d+$|^-\d*\.\d+$')

That's too restrictive.

^-\d+$|^-\d*\.\d+$|^-\d*\.?\d+e[+-]?\d+$

would be appropriate and actually cover most negative number formats

Your environment

Python 3.11.3 (F37 x86), but also CPython main (v3.12.0b1-321-g8da9d1b163)

Impact

Yeah, it's annoying, because we can't go out and repair deployed code, monkey-patch everyone's parser. This leads to surprising outages.

The proposed alternative regex might be made default, with the option to switch back to the over-restrictive parser if arguments starting with -\d*\.?\d+e are registered (which, honestly, sounds unlikely).

Related issue

#53580 which I found is related, but less solution-oriented seems to have been closed as "WONTFIX" with the slightly vague argument that the change is hard (based on proposing an overly simple regex). Seeing my proposal, hm, not sure I agree.

Alternatives

Alternative Regexes

(that regex I propose might not be optional. If we can actually find the regex that describes negative floating point numbers, that's what I'd propose we use)

Passing of a user-defined negative-number regex

Since monkeypatching does work, at least for the main parser, a colleague brought up the idea of adding a keyword argument to ArgumentParser which would allow for user-defined modification of that string, at zero risk of breaking existing use cases

Linked PRs

@marcusmueller marcusmueller added the type-bug An unexpected behavior, bug, or error label Jun 12, 2023
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Jun 12, 2023
@terryjreedy terryjreedy moved this to Bugs in Argparse issues Aug 16, 2023
marcusmueller added a commit to marcusmueller/cpython that referenced this issue Feb 1, 2024
…arguments

previously, type=float values like -3e12 would be misinterpreted as -option

Signed-off-by: Marcus Müller <[email protected]>
marcusmueller added a commit to marcusmueller/cpython that referenced this issue Feb 1, 2024
…arguments

previously, type=float values like -3e12 would be misinterpreted as -option

Signed-off-by: Marcus Müller <[email protected]>
@vadmium
Copy link
Member

vadmium commented May 4, 2024

Same bug as #66862 and #96994

@hugovk
Copy link
Member

hugovk commented Jun 15, 2024

Closing as duplicate.

@hugovk hugovk closed this as not planned Won't fix, can't repro, duplicate, stale Jun 15, 2024
@github-project-automation github-project-automation bot moved this from Bugs to Doc issues in Argparse issues Jun 15, 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

4 participants