https://github.com/python/cpython/commit/dc48312717142ec902197da504fad333f13c9937 commit: dc48312717142ec902197da504fad333f13c9937 branch: main author: Savannah Ostrowski <[email protected]> committer: gpshead <[email protected]> date: 2024-09-23T11:16:55-07:00 summary:
GH-124321: Fix argparse negative number parsing to capture -.5(GH-124322) Co-authored-by: Adam Turner <[email protected]> files: M Lib/argparse.py M Lib/test/test_argparse.py diff --git a/Lib/argparse.py b/Lib/argparse.py index 7988c447d03584..b77da29417b920 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[\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 diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 138ff19e86acf4..37d07968b22543 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -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): _______________________________________________ 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]
