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

GH-124321: Argparse negative number parsing does not capture -.5 #124322

Merged
merged 3 commits into from
Sep 23, 2024

Conversation

savannahostrowski
Copy link
Member

@savannahostrowski savannahostrowski commented Sep 22, 2024

@savannahostrowski
Copy link
Member Author

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+)*)$')
Copy link
Member

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

Copy link
Member

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)

Lib/test/test_argparse.py Show resolved Hide resolved
Lib/argparse.py Outdated Show resolved Hide resolved
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@gpshead gpshead merged commit dc48312 into python:main Sep 23, 2024
32 checks passed
@miss-islington-app
Copy link

Thanks @savannahostrowski for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @savannahostrowski and @gpshead, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker dc48312717142ec902197da504fad333f13c9937 3.13

@miss-islington-app
Copy link

Sorry, @savannahostrowski and @gpshead, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker dc48312717142ec902197da504fad333f13c9937 3.12

@gpshead
Copy link
Member

gpshead commented Sep 23, 2024

(in sprint room chat: Savannah is looking into the backports)

savannahostrowski added a commit to savannahostrowski/cpython that referenced this pull request Sep 23, 2024
…ythonGH-124322)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
(cherry picked from commit dc48312)
savannahostrowski added a commit to savannahostrowski/cpython that referenced this pull request Sep 23, 2024
…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>
savannahostrowski added a commit to savannahostrowski/cpython that referenced this pull request Sep 23, 2024
…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>
@serhiy-storchaka
Copy link
Member

I am not sure that this change (and the previous one) should be backported.

@bedevere-app
Copy link

bedevere-app bot commented Sep 23, 2024

GH-124361 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Sep 23, 2024
@bedevere-app
Copy link

bedevere-app bot commented Sep 23, 2024

GH-124359 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Sep 23, 2024
@savannahostrowski savannahostrowski deleted the negative_arg_test branch September 27, 2024 16:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants