Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
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+$')
self._negative_number_matcher = _re.compile(r'^-\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
9 changes: 8 additions & 1 deletion Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,9 @@ class TestOptionLike(ParserTestCase):
failures = ['-x', '-y2.5', '-xa', '-x -a',
'-x -3', '-x -3.5', '-3 -3.5',
'-x -2.5', '-x -2.5 a', '-3 -.5',
'a x -1', '-x -1 a', '-3 -1 a']
'a x -1', '-x -1 a', '-3 -1 a',
'-x -_.45 -3 1_000', '-x -1_000.0_45 -3 1_000',
'-x -1__000_000.0 -3 1_000_000',]
successes = [
('', NS(x=None, y=None, z=[])),
('-x 2.5', NS(x=2.5, y=None, z=[])),
Expand All @@ -1506,6 +1508,11 @@ class TestOptionLike(ParserTestCase):
('a -x 1', NS(x=1.0, y=None, z=['a'])),
('-x 1 a', NS(x=1.0, y=None, z=['a'])),
('-3 1 a', NS(x=None, y=1.0, z=['a'])),
('-x-1_000.0 -3 1_000.0', NS(x=-1000.0, y=1000.0, z=[])),
('-x-1_000 -3 1_000', NS(x=-1000, y=1000, z=[])),
('-x-1_000_000.0 -3 1_000_000.0', NS(x=-1000000.0, y=1000000.0, z=[])),
('-x-1_000_000_000.0 -3 1_000_000_000.0', NS(x=-1000000000.0, y=1000000000.0, z=[])),

]


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