Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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+(?:_\d+)*)?|\.\d+(?:_\d+)*)$')
self._negative_number_matcher = _re.compile(r'-\.?\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
27 changes: 20 additions & 7 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2188,20 +2188,33 @@ class TestNegativeNumber(ParserTestCase):
argument_signatures = [
Sig('--int', type=int),
Sig('--float', type=float),
Sig('--complex', type=complex),
]
failures = [
'--float -_.45',
'--float -1__000.0',
'--float -1.0.0',
'--int -1__000',
'--int -1.0',
'--complex -1__000.0j',
'--complex -1.0jj',
'--complex -_.45j',
]
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)),
('--float -.5', NS(int=None, float=-0.5)),
('--float -.5_000', NS(int=None, float=-0.5)),
('--int -1000 --float -1000.0', NS(int=-1000, float=-1000.0, complex=None)),
('--int -1_000 --float -1_000.0', NS(int=-1000, float=-1000.0, complex=None)),
('--int -1_000_000 --float -1_000_000.0', NS(int=-1000000, float=-1000000.0, complex=None)),
('--float -1_000.0', NS(int=None, float=-1000.0, complex=None)),
('--float -1_000_000.0_0', NS(int=None, float=-1000000.0, complex=None)),
('--float -.5', NS(int=None, float=-0.5, complex=None)),
('--float -.5_000', NS(int=None, float=-0.5, complex=None)),
('--float -1e3', NS(int=None, float=-1000, complex=None)),
('--float -1e-3', NS(int=None, float=-0.001, complex=None)),
('--complex -1j', NS(int=None, float=None, complex=-1j)),
('--complex -1_000j', NS(int=None, float=None, complex=-1000j)),
('--complex -1_000.0j', NS(int=None, float=None, complex=-1000.0j)),
('--complex -1e3j', NS(int=None, float=None, complex=-1000j)),
('--complex -1e-3j', NS(int=None, float=None, complex=-0.001j)),
]

class TestInvalidAction(TestCase):
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 complex numbers or negative numbers using scientific notation.
Loading