8000 GH-124693: Support parsing negative scientific and complex numbers ar… · python/cpython@0c5a48c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c5a48c

Browse files
GH-124693: Support parsing negative scientific and complex numbers argparse (GH-124823)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 52f70da commit 0c5a48c

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

Lib/argparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ def __init__(self,
13671367
self._defaults = {}
13681368

13691369
# determines whether an "option" looks like a negative number
1370-
self._negative_number_matcher = _re.compile(r'^-(?:\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?|\.\d+(?:_\d+)*)$')
1370+
self._negative_number_matcher = _re.compile(r'-\.?\d')
13711371

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

Lib/test/test_argparse.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,20 +2195,33 @@ class TestNegativeNumber(ParserTestCase):
21952195
argument_signatures = [
21962196
Sig('--int', type=int),
21972197
Sig('--float', type=float),
2198+
Sig('--complex', type=complex),
21982199
]
21992200
failures = [
22002201
'--float -_.45',
22012202
'--float -1__000.0',
2203+
'--float -1.0.0',
22022204
'--int -1__000',
2205+
'--int -1.0',
2206+
'--complex -1__000.0j',
2207+
'--complex -1.0jj',
2208+
'--complex -_.45j',
22032209
]
22042210
successes = [
2205-
('--int -1000 --float -1000.0', NS(int=-1000, float=-1000.0)),
2206-
('--int -1_000 --float -1_000.0', NS(int=-1000, float=-1000.0)),
2207-
('--int -1_000_000 --float -1_000_000.0', NS(int=-1000000, float=-1000000.0)),
2208-
('--float -1_000.0', NS(int=None, float=-1000.0)),
2209-
('--float -1_000_000.0_0', NS(int=None, float=-1000000.0)),
2210-
('--float -.5', NS(int=None, float=-0.5)),
2211-
('--float -.5_000', NS(int=None, float=-0.5)),
2211+
('--int -1000 --float -1000.0', NS(int=-1000, float=-1000.0, complex=None)),
2212+
('--int -1_000 --float -1_000.0', NS(int=-1000, float=-1000.0, complex=None)),
2213+
('--int -1_000_000 --float -1_000_000.0', NS(int=-1000000, float=-1000000.0, complex=None)),
2214+
('--float -1_000.0', NS(int=None, float=-1000.0, complex=None)),
2215+
('--float -1_000_000.0_0', NS(int=None, float=-1000000.0, complex=None)),
2216+
('--float -.5', NS(int=None, float=-0.5, complex=None)),
2217+
('--float -.5_000', NS(int=None, float=-0.5, complex=None)),
2218+
('--float -1e3', NS(int=None, float=-1000, complex=None)),
2219+
('--float -1e-3', NS(int=None, float=-0.001, complex=None)),
2220+
('--complex -1j', NS(int=None, float=None, complex=-1j)),
2221+
('--complex -1_000j', NS(int=None, float=None, complex=-1000j)),
2222+
('--complex -1_000.0j', NS(int=None, float=None, complex=-1000.0j)),
2223+
('--complex -1e3j', NS(int=None, float=None, complex=-1000j)),
2224+
('--complex -1e-3j', NS(int=None, float=None, complex=-0.001j)),
22122225
]
22132226

22142227
class TestInvalidAction(TestCase):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug where :mod:`argparse` doesn't recognize negative complex numbers or negative numbers using scientific notation.

0 commit comments

Comments
 (0)
0