From f09ef423c572a43743e72cec65e6a2cd600aaa01 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 10 Sep 2024 20:10:09 -0700 Subject: [PATCH 1/3] Update handling of nargs=* being incorrectly required --- Lib/argparse.py | 4 +++- Lib/test/test_argparse.py | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index 100ef9f55cd2f7..b3087d111b64a6 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -2130,7 +2130,9 @@ def consume_positionals(start_index): required_actions = [] for action in self._actions: if action not in seen_actions: - if action.required: + if (action.required and + action.default is None and + action.nargs not in (ZERO_OR_MORE, OPTIONAL)): required_actions.append(_get_action_name(action)) else: # Convert action default now instead of doing it before diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index fd111be18aed6e..0e8b089011ec8c 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -6091,6 +6091,13 @@ def test_required_args(self): 'the following arguments are required: bar, baz', self.parser.parse_args, []) + def test_required_and_nargs(self): + self.parser.add_argument('bar') + self.parser.add_argument('baz', nargs='*') + self.assertRaisesRegex(argparse.ArgumentError, + 'the following arguments are required: bar', + self.parser.parse_args, []) + def test_required_mutually_exclusive_args(self): group = self.parser.add_mutually_exclusive_group(required=True) group.add_argument('--bar') From dfef7d13ed042ecbf7b802f1d156b9da92462599 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 11 Sep 2024 03:21:58 +0000 Subject: [PATCH 2/3] =?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-09-11-03-21-56.gh-issue-103219.F5atBD.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-09-11-03-21-56.gh-issue-103219.F5atBD.rst diff --git a/Misc/NEWS.d/next/Library/2024-09-11-03-21-56.gh-issue-103219.F5atBD.rst b/Misc/NEWS.d/next/Library/2024-09-11-03-21-56.gh-issue-103219.F5atBD.rst new file mode 100644 index 00000000000000..b35be7b69ec4dd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-09-11-03-21-56.gh-issue-103219.F5atBD.rst @@ -0,0 +1 @@ +Fixes a bug in argparse where optional args are incorrectly processed as required args From d9d1d38534efd4fc7859bfd4dfdc1c6e81c57f47 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 10 Sep 2024 20:23:31 -0700 Subject: [PATCH 3/3] Update test name --- Lib/test/test_argparse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 0e8b089011ec8c..9e313ca9c72a9a 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -6091,7 +6091,7 @@ def test_required_args(self): 'the following arguments are required: bar, baz', self.parser.parse_args, []) - def test_required_and_nargs(self): + def test_required_and_optional(self): self.parser.add_argument('bar') self.parser.add_argument('baz', nargs='*') self.assertRaisesRegex(argparse.ArgumentError,