-
-
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
GH-124321: Argparse negative number parsing does not capture -.5 #124322
Conversation
Adding skip news here since there's already a news entry in the original PR. This just addresses another case where we had a test missing. |
Lib/argparse.py
Outdated
@@ -1360,7 +1360,7 @@ def __init__(self, | |||
self._defaults = {} | |||
|
|||
# determines whether an "option" looks like a negative number | |||
self._negative_number_matcher = _re.compile(r'^-\d[\d_]*(\.\d[\d_]*)?$') | |||
self._negative_number_matcher = _re.compile(r'^-(\d+(_\d+)*(\.\d+(_\d+)*)?|\.\d+(_\d+)*)$') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern looks fine for capturing negatives without a leading digit, but it does have a (rather silly) breaking change: -12__3
was accepted by the old pattern but not the new.
I think support for runs of underscores was accidental, though (and undocumented), so it seems fine to remove it. Would be remiss not to point it out, though!
A
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my gut feeling: we don't need to care if this is pedantically accurate? regexes are not great parsers. if a value that fails the actual parser makes it through, it'll be an SyntaxError/ValueError upon int()
conversion. do we capture the exception within argparse at a reasonable place such that the error is reported to the user in a friendly manner associated with the argument? (anyways, I'd leave that for a followup issue if not)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Thanks @savannahostrowski for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
Sorry, @savannahostrowski and @gpshead, I could not cleanly backport this to
|
Sorry, @savannahostrowski and @gpshead, I could not cleanly backport this to
|
(in sprint room chat: Savannah is looking into the backports) |
…ythonGH-124322) Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> (cherry picked from commit dc48312)
…re -.5(pythonGH-124322) (cherry picked from commit dc48312) Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com> Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
…re -.5(pythonGH-124322) (cherry picked from commit dc48312) Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com> Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
I am not sure that this change (and the previous one) should be backported. |
GH-124361 is a backport of this pull request to the 3.12 branch. |
GH-124359 is a backport of this pull request to the 3.13 branch. |
This is a follow-up to #123970 per https://github.com/python/cpython/pull/123970/files#r1770614169