From e071225c0fa6c20760e3cd500236434cfc6abc82 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Sat, 19 Apr 2025 15:02:11 +0300 Subject: [PATCH 1/4] add test cases --- Lib/test/test_argparse.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 488a3a4ed20fac..394c78ab14726c 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -607,6 +607,25 @@ class TestOptionalsNargsDefault(ParserTestCase): ('-x a', NS(x='a')), ] +class TestNargsDefaultConvert(ParserTestCase): + """Tests not specifying the number of args for an Optional""" + + argument_signatures = [Sig('-x', type=int, nargs='+', default=['1', '2'])] + failures = [] + successes = [ + ('', NS(x=[1, 2])), + ] + + +class TestListDefaultConvert(ParserTestCase): + """Tests not specifying the number of args for an Optional""" + + argument_signatures = [Sig('-x', type=int, default=['1', '2'])] + failures = [] + successes = [ + ('', NS(x=['1', '2'])), + ] + class TestOptionalsNargs1(ParserTestCase): """Tests specifying 1 arg for an Optional""" From 34fb834c5362de603eca2e8d38e86651d6145af3 Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Sat, 19 Apr 2025 15:03:03 +0300 Subject: [PATCH 2/4] process list of strings for nargs default args --- Lib/argparse.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index d24fa72e573d4f..51c43a3f07a99d 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -2237,11 +2237,16 @@ def consume_positionals(start_index): # twice (which may fail) if the argument was given, but # only if it was defined already in the namespace if (action.default is not None and - isinstance(action.default, str) and hasattr(namespace, action.dest) and action.default is getattr(namespace, action.dest)): - setattr(namespace, action.dest, + if isinstance(action.default, str): + setattr(namespace, action.dest, self._get_value(action, action.default)) + elif (action.nargs not in (OPTIONAL, None) and + isinstance(action.default, (list, tuple)) and + all(isinstance(v, str) for v in action.default)): + setattr(namespace, action.dest, + [self._get_value(action, v) for v in action.default]) if required_actions: raise ArgumentError(None, _('the following arguments are required: %s') % From 4f8ef917bc0dfdb893ee9df9089a3daf22dfeeac Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 19 Apr 2025 12:16:00 +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/2025-04-19-12-15-54.gh-issue-132717.TJQ9_b.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-04-19-12-15-54.gh-issue-132717.TJQ9_b.rst diff --git a/Misc/NEWS.d/next/Library/2025-04-19-12-15-54.gh-issue-132717.TJQ9_b.rst b/Misc/NEWS.d/next/Library/2025-04-19-12-15-54.gh-issue-132717.TJQ9_b.rst new file mode 100644 index 00000000000000..f314cbbc607deb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-19-12-15-54.gh-issue-132717.TJQ9_b.rst @@ -0,0 +1 @@ +`:mod:`argparse` now calls the convert function for default values in variadic arguments. Patch by Noam Cohen. From c62037dabd5885383060cf0c91aec2e2bc5a5bec Mon Sep 17 00:00:00 2001 From: Noam Cohen Date: Sat, 19 Apr 2025 17:21:18 +0300 Subject: [PATCH 4/4] Fix blurb entry --- .../Library/2025-04-19-12-15-54.gh-issue-132717.TJQ9_b.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-04-19-12-15-54.gh-issue-132717.TJQ9_b.rst b/Misc/NEWS.d/next/Library/2025-04-19-12-15-54.gh-issue-132717.TJQ9_b.rst index f314cbbc607deb..949fcb9d7119c8 100644 --- a/Misc/NEWS.d/next/Library/2025-04-19-12-15-54.gh-issue-132717.TJQ9_b.rst +++ b/Misc/NEWS.d/next/Library/2025-04-19-12-15-54.gh-issue-132717.TJQ9_b.rst @@ -1 +1,2 @@ -`:mod:`argparse` now calls the convert function for default values in variadic arguments. Patch by Noam Cohen. +:mod:`argparse` now calls the convert function for default values in variadic +arguments. Patch by Noam Cohen.