From 1d3caa02db54f6a2a01bbc6ae2687190b8044532 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Mon, 30 Sep 2024 19:22:40 -0700 Subject: [PATCH 1/4] Make regex more lenient to allow complex and scientific --- Lib/argparse.py | 2 +- Lib/test/test_argparse.py | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index 874f271959c4fe..4dad207f91e487 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+)*)?|\.\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 diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index a972ed0cc9053b..ba8cf92c08f352 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2188,20 +2188,30 @@ class TestNegativeNumber(ParserTestCase): argument_signatures = [ Sig('--int', type=int), Sig('--float', type=float), + Sig('--complex', type=complex), ] failures = [ '--float -_.45', '--float -1__000.0', '--int -1__000', + '--int -1.0', + '--complex -1__000.0j', ] 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): From 72d3ba3520b1c61495e207ce2def7ad322136f6b Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Mon, 30 Sep 2024 19:26:26 -0700 Subject: [PATCH 2/4] Add more test cases --- Lib/test/test_argparse.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index ba8cf92c08f352..d5ac95ab3d72dc 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2193,9 +2193,12 @@ class TestNegativeNumber(ParserTestCase): 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, complex=None)), From 46768b91119d1c92657253d7041b6cf3d1801001 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 02:31:16 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-10-01-02-31-13.gh-issue-124693.qzbXKB.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-10-01-02-31-13.gh-issue-124693.qzbXKB.rst diff --git a/Misc/NEWS.d/next/Library/2024-10-01-02-31-13.gh-issue-124693.qzbXKB.rst b/Misc/NEWS.d/next/Library/2024-10-01-02-31-13.gh-issue-124693.qzbXKB.rst new file mode 100644 index 00000000000000..483fbeda681306 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-01-02-31-13.gh-issue-124693.qzbXKB.rst @@ -0,0 +1 @@ +Fix a bug where :mod`argparse` doesn't recognize negative complex numbers or negative numbers using scientific notation From 0fd24bc1823d3887ddc85f1c24bafe70e31bddd2 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Mon, 30 Sep 2024 19:38:32 -0700 Subject: [PATCH 4/4] Fix broken Sphinx --- .../next/Library/2024-10-01-02-31-13.gh-issue-124693.qzbXKB.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-10-01-02-31-13.gh-issue-124693.qzbXKB.rst b/Misc/NEWS.d/next/Library/2024-10-01-02-31-13.gh-issue-124693.qzbXKB.rst index 483fbeda681306..3e87eb457d9911 100644 --- a/Misc/NEWS.d/next/Library/2024-10-01-02-31-13.gh-issue-124693.qzbXKB.rst +++ b/Misc/NEWS.d/next/Library/2024-10-01-02-31-13.gh-issue-124693.qzbXKB.rst @@ -1 +1 @@ -Fix a bug where :mod`argparse` doesn't recognize negative complex numbers or negative numbers using scientific notation +Fix a bug where :mod:`argparse` doesn't recognize negative complex numbers or negative numbers using scientific notation.