From b6a18bb212580fc2e33851363bceddaf9256ae0e Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 17 Jun 2020 00:46:21 +0200 Subject: [PATCH 1/2] bpo-22433: do not consider "--foo='bar baz'" to be a positional argument Ref: https://bugs.python.org/issue22433 --- Lib/argparse.py | 4 ++-- Lib/test/test_argparse.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index 2fb1da59f942cf..d4b6fb53af8e11 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -2223,8 +2223,8 @@ def _parse_optional(self, arg_string): if not self._has_negative_number_optionals: return None - # if it contains a space, it was meant to be a positional - if ' ' in arg_string: + # if it contains a space (before any equal sign), it was meant to be a positional + if ' ' in arg_string.split("=", 1)[0]: return None # it was meant to be an optional but there is no such option diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 22cae626ccc297..60a18b00806892 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2018,6 +2018,15 @@ def test_parse_known_args(self): self.parser.parse_known_args('0.5 -W 1 b -X Y -w 7 Z'.split()), (NS(foo=False, bar=0.5, w=7, x='b'), ['-W', '-X', 'Y', 'Z']), ) + self.assertEqual( + self.parser.parse_known_args(['0.5', '--opt="with space"']), + (NS(foo=False, bar=0.5), ['--opt="with space"']), + ) + self.assertRaisesRegexp( + ArgumentParserError, + "invalid choice: '--opt with space'", + self.parser.parse_known_args, + ['0.5', '--opt with space']) def test_dest(self): parser = ErrorRaisingArgumentParser() From 6c2d2033f066ce9bdda72c018cad031f8dafb69c Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 6 Jul 2020 15:25:47 +0200 Subject: [PATCH 2/2] fixup! bpo-22433: do not consider "--foo='bar baz'" to be a positional argument --- Lib/argparse.py | 3 ++- Lib/test/test_argparse.py | 2 +- .../next/Library/2020-07-06-15-25-29.bpo-22433.JBnwdX.rst | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-07-06-15-25-29.bpo-22433.JBnwdX.rst diff --git a/Lib/argparse.py b/Lib/argparse.py index d4b6fb53af8e11..acd3154e681e0e 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -2223,7 +2223,8 @@ def _parse_optional(self, arg_string): if not self._has_negative_number_optionals: return None - # if it contains a space (before any equal sign), it was meant to be a positional + # if it contains a space (before any equal sign), it was meant to be a + # positional if ' ' in arg_string.split("=", 1)[0]: return None diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 60a18b00806892..f378606cdecbd3 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2022,7 +2022,7 @@ def test_parse_known_args(self): self.parser.parse_known_args(['0.5', '--opt="with space"']), (NS(foo=False, bar=0.5), ['--opt="with space"']), ) - self.assertRaisesRegexp( + self.assertRaisesRegex( ArgumentParserError, "invalid choice: '--opt with space'", self.parser.parse_known_args, diff --git a/Misc/NEWS.d/next/Library/2020-07-06-15-25-29.bpo-22433.JBnwdX.rst b/Misc/NEWS.d/next/Library/2020-07-06-15-25-29.bpo-22433.JBnwdX.rst new file mode 100644 index 00000000000000..aa0adb5a732987 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-07-06-15-25-29.bpo-22433.JBnwdX.rst @@ -0,0 +1,2 @@ +Fixed handling of "--foo='bar baz'" as non-positional argument with +:mod:`argparse`.