https://github.com/python/cpython/commit/14e5bdceff45e6e789e1f838b96988946c75b0f4
commit: 14e5bdceff45e6e789e1f838b96988946c75b0f4
branch: main
author: Savannah Ostrowski <[email protected]>
committer: hauntsaninja <[email protected]>
date: 2024-09-16T23:30:17-07:00
summary:

GH-123945: Update regex for parsing negative numbers that contain underscores 
(#123970)

---------

Co-authored-by: Brandt Bucher <[email protected]>
Co-authored-by: Shantanu <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-09-11-19-05-32.gh-issue-123945.jLwybB.rst
M Lib/argparse.py
M Lib/test/test_argparse.py

diff --git a/Lib/argparse.py b/Lib/argparse.py
index 100ef9f55cd2f7..a88a8c65c40a1d 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -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[\d_]*)?$')
 
         # whether or not there are any optionals that look like negative
         # numbers -- uses a list so it can be shared and edited
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index fd111be18aed6e..584462b741ee99 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -2093,6 +2093,26 @@ class TestActionExtend(ParserTestCase):
     ]
 
 
+class TestNegativeNumber(ParserTestCase):
+    """Test parsing negative numbers"""
+
+    argument_signatures = [
+        Sig('--int', type=int),
+        Sig('--float', type=float),
+    ]
+    failures = [
+        '--float -_.45',
+        '--float -1__000.0',
+        '--int -1__000',
+    ]
+    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)),
+    ]
+
 class TestInvalidAction(TestCase):
     """Test invalid user defined Action"""
 
diff --git 
a/Misc/NEWS.d/next/Library/2024-09-11-19-05-32.gh-issue-123945.jLwybB.rst 
b/Misc/NEWS.d/next/Library/2024-09-11-19-05-32.gh-issue-123945.jLwybB.rst
new file mode 100644
index 00000000000000..26b0ac80b1b3fd
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-09-11-19-05-32.gh-issue-123945.jLwybB.rst
@@ -0,0 +1 @@
+Fix a bug where :mod:`argparse` doesn't recognize negative numbers with 
underscores

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to