8000 [3.13] GH-123945: Update regex for parsing negative numbers that contain underscores (GH-123970) by miss-islington · Pull Request #124158 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.13] GH-123945: Update regex for parsing negative numbers that contain underscores (GH-123970) #124158

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
GH-123945: Update regex for parsing negative numbers that contain und…
…erscores (GH-123970)

---------

(cherry picked from commit 14e5bdc)

Co-authored-by: Savannah Ostrowski <savannahostrowski@gmail.com>
Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
  • Loading branch information
3 people authored and miss-islington committed Sep 17, 2024
commit b766f3c8fe438984c6d20e7ae35108ceaf55e832
2 changes: 1 addition & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,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+$')
self._negative_number_matcher = _re.compile(r'^-\d[\d_]*(\.\d[\d_]*)?$')

# whether or not there are any optionals that look like negative
# numbers -- uses a list so it can be shared and edited
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2136,6 +2136,26 @@ class TestActionExtend(ParserTestCase):
]


class TestNegativeNumber(ParserTestCase):
"""Test parsing negative numbers"""

argument_signatures = [
Sig('--int', type=int),
Sig('--float', type=float),
]
failures = [
'--float -_.45',
'--float -1__000.0',
'--int -1__000',
]
successes = [
('--int -1000 --float -1000.0', NS(int=-1000, float=-1000.0)),
('--int -1_000 --float -1_000.0', NS(int=-1000, float=-1000.0)),
('--int -1_000_000 --float -1_000_000.0', NS(int=-1000000, float=-1000000.0)),
('--float -1_000.0', NS(int=None, float=-1000.0)),
('--float -1_000_000.0_0', NS(int=None, float=-1000000.0)),
]

class TestInvalidAction(TestCase):
"""Test invalid user defined Action"""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where :mod:`argparse` doesn't recognize negative numbers with underscores
Loading
0