]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-124321: Fix argparse negative number parsing to capture -.5(GH-124322)
authorSavannah Ostrowski <savannahostrowski@gmail.com>
Mon, 23 Sep 2024 18:16:55 +0000 (11:16 -0700)
committerGitHub <noreply@github.com>
Mon, 23 Sep 2024 18:16:55 +0000 (11:16 -0700)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Lib/argparse.py
Lib/test/test_argparse.py

index 7988c447d0358400d0bd002f98271af419bd99b2..b77da29417b920f2911e69699178fa23f4bd00f0 100644 (file)
@@ -1360,7 +1360,7 @@ class _ActionsContainer(object):
         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
index 138ff19e86acf47b37e725533bb7ca0d799e63fd..37d07968b225430918620990ab21c8fef90b5088 100644 (file)
@@ -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):