Skip to content

Commit

Permalink
pythonGH-124321: Fix argparse negative number parsing to capture -.5(p…
Browse files Browse the repository at this point in the history
…ythonGH-124322)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
  • Loading branch information
savannahostrowski and AA-Turner authored Sep 23, 2024
1 parent 2f6d410 commit dc48312
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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+)*)$')

# whether or not there are any optionals that look like negative
# numbers -- uses a list so it can be shared and edited
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,8 @@ class TestNegativeNumber(ParserTestCase):
('--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)),
('--float -.5', NS(int=None, float=-0.5)),
('--float -.5_000', NS(int=None, float=-0.5)),
]

class TestInvalidAction(TestCase):
Expand Down

0 comments on commit dc48312

Please sign in to comment.