-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Argparse does not identify negative numbers with underscores as a numerical value #123945
Comments
Small reproducer: import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--int", type=int)
parser.add_argument("--float", type=float)
args = parser.parse_args()
print(args) Reproducible on ❯ p 1.py --int 1000
Namespace(int=1000, float=None)
❯ p 1.py --int 1_000
Namespace(int=1000, float=None)
❯ p 1.py --int -1000
Namespace(int=-1000, float=None)
❯ p 1.py --int -1_000
usage: 1.py [-h] [--int INT] [--float FLOAT]
1.py: error: argument --int: expected one argument ❯ p 1.py --float 1000.1
Namespace(int=None, float=1000.1)
❯ p 1.py --float 1_000.1
Namespace(int=None, float=1000.1)
❯ p 1.py --float -1000.1
Namespace(int=None, float=-1000.1)
❯ p 1.py --float -1_000.1
usage: 1.py [-h] [--int INT] [--float FLOAT]
1.py: error: argument --float: expected one argument |
I'll take a look at this! |
Thanks for picking this up. Just a quick note on the proposed regex: The proposed regex may accept numbers like: There is also no provision for underscores in the fractional part of the number. |
…erscores (#123970) --------- Co-authored-by: Brandt Bucher <[email protected]> Co-authored-by: Shantanu <[email protected]>
…in underscores (pythonGH-123970) --------- (cherry picked from commit 14e5bdc) Co-authored-by: Savannah Ostrowski <[email protected]> Co-authored-by: Brandt Bucher <[email protected]> Co-authored-by: Shantanu <[email protected]>
…in underscores (python#123970) --------- Co-authored-by: Brandt Bucher <[email protected]> Co-authored-by: Shantanu <[email protected]>
The last change introduced a regression: |
I am not actually sure that this issue should be classified as a bug. There was no promise that argparse supports all numbers supported by Python (and it does not support numbers in scientific notations like |
Yeah, I agree, let me close the backports. |
Okay great, looks like Savannah fixed the regression in #124321 , so I think we can call this one closed 🎉 |
fwiw that makes sense, supporting parsing these is more "feature-ish". thanks for closing the backport PRs. |
I wonder whether we should make the check even more lenient, for example |
At some point I needed something to accept floats using all kind of notations. So what I did is replace the action by some |
@serhiy-storchaka Right now, we actually do accept numbers like positive versions of scientific notation and complex numbers...but not negative (we didn't before either). I will open a PR to get us to parity. |
Bug report
Bug description:
Simple example:
$ python test_arguments.py --Value -1_000.0
error: argument -v/--Value: expected one argument
The regular expression to identify negative numbers in the argparse.py module uses the following regular expression:
This does not identify negative numerical values with underscores as a numerical value
This could probably be resolved by changing the regex to:
CPython versions tested on:
3.9
Operating systems tested on:
Windows
Linked PRs
The text was updated successfully, but these errors were encountered: