Skip to content

Commit dc48312

Browse files
GH-124321: Fix argparse negative number parsing to capture -.5(GH-124322)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
1 parent 2f6d410 commit dc48312

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

Lib/argparse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ def __init__(self,
13601360
self._defaults = {}
13611361

13621362
# determines whether an "option" looks like a negative number
1363-
self._negative_number_matcher = _re.compile(r'^-\d[\d_]*(\.\d[\d_]*)?$')
1363+
self._negative_number_matcher = _re.compile(r'^-(?:\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?|\.\d+(?:_\d+)*)$')
13641364

13651365
# whether or not there are any optionals that look like negative
13661366
# numbers -- uses a list so it can be shared and edited

Lib/test/test_argparse.py

+2
Original file line numberDiff line numberDiff line change
@@ -2111,6 +2111,8 @@ class TestNegativeNumber(ParserTestCase):
21112111
('--int -1_000_000 --float -1_000_000.0', NS(int=-1000000, float=-1000000.0)),
21122112
('--float -1_000.0', NS(int=None, float=-1000.0)),
21132113
('--float -1_000_000.0_0', NS(int=None, float=-1000000.0)),
2114+
('--float -.5', NS(int=None, float=-0.5)),
2115+
('--float -.5_000', NS(int=None, float=-0.5)),
21142116
]
21152117

21162118
class TestInvalidAction(TestCase):

0 commit comments

Comments
 (0)