From 3491edaa6644645a894a40d2fd646ea0d1396f93 Mon Sep 17 00:00:00 2001 From: Swayam Shah Date: Sat, 7 Jun 2025 19:32:52 +0530 Subject: [PATCH 1/8] fix parser --- Lib/argparse.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index 83258cf3e0f37d..c1196d60ffaf99 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1259,17 +1259,16 @@ def add_parser(self, name, *, deprecated=False, **kwargs): if alias in self._name_parser_map: raise ValueError(f'conflicting subparser alias: {alias}') - # create a pseudo-action to hold the choice help - if 'help' in kwargs: - help = kwargs.pop('help') - choice_action = self._ChoicesPseudoAction(name, aliases, help) - self._choices_actions.append(choice_action) - else: - choice_action = None + # Handle help/description logic + help = kwargs.pop('help', None) + if 'description' not in kwargs and help is not None: + kwargs['description'] = help - # create the parser and add it to the map + # Create the parser and pseudo-action parser = self._parser_class(**kwargs) - if choice_action is not None: + if help is not None: + choice_action = self._ChoicesPseudoAction(name, aliases, help) + self._choices_actions.append(choice_action) parser._check_help(choice_action) self._name_parser_map[name] = parser From e26157afccfcc08d7358ef8bfa6bec1d8d0cef5c Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 7 Jun 2025 15:10:23 +0000 Subject: [PATCH 2/8] =?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-06-07-15-10-22.gh-issue-135227.LPeDGt.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst diff --git a/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst b/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst new file mode 100644 index 00000000000000..e73231437585c0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst @@ -0,0 +1,2 @@ +- ArgumentParser: ``add_parser()`` now uses the ``help`` value as the + default ``description`` if none is provided. Patch by Swayam. From 87377595bc658a3d98c51037a9b6ad58e648a479 Mon Sep 17 00:00:00 2001 From: Swayam Shah Date: Sat, 7 Jun 2025 20:48:15 +0530 Subject: [PATCH 3/8] fix docs fail --- .../next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst b/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst index e73231437585c0..0cd7099dfd8415 100644 --- a/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst +++ b/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst @@ -1,2 +1,2 @@ -- ArgumentParser: ``add_parser()`` now uses the ``help`` value as the +ArgumentParser: ``add_parser()`` now uses the ``help`` value as the default ``description`` if none is provided. Patch by Swayam. From ff07923c41f0a1d6776745a2079e9862d1d27997 Mon Sep 17 00:00:00 2001 From: Swayam Shah Date: Sat, 7 Jun 2025 21:12:25 +0530 Subject: [PATCH 4/8] correct format --- .../Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst b/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst index 0cd7099dfd8415..df080803a70f6d 100644 --- a/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst +++ b/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst @@ -1,2 +1,2 @@ -ArgumentParser: ``add_parser()`` now uses the ``help`` value as the - default ``description`` if none is provided. Patch by Swayam. +:meth:`argparse.ArgumentParser.add_argument` now uses the *help* value as the +default *description* if none is provided. Patch by Swayam. \ No newline at end of file From 6990b9e95c012faa56905a9b1d75d562e79f61e1 Mon Sep 17 00:00:00 2001 From: Swayam Shah Date: Sat, 7 Jun 2025 21:14:28 +0530 Subject: [PATCH 5/8] fix format --- .../Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst b/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst index df080803a70f6d..fc43d2ef37a7f0 100644 --- a/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst +++ b/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst @@ -1,2 +1,3 @@ -:meth:`argparse.ArgumentParser.add_argument` now uses the *help* value as the -default *description* if none is provided. Patch by Swayam. \ No newline at end of file +The :meth:`!add_parser` method of the special action object returned by +:meth:`argparse.ArgumentParser.add_subparsers` now uses the *help* value as +the default *description* if none is provided. Patch by Swayam. \ No newline at end of file From 1cc6aad75e1b065ad1bb70a1dd520ffa91db913a Mon Sep 17 00:00:00 2001 From: Swayam Shah Date: Sat, 7 Jun 2025 21:16:26 +0530 Subject: [PATCH 6/8] fix format1 --- .../next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst b/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst index fc43d2ef37a7f0..2a936933c826ba 100644 --- a/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst +++ b/Misc/NEWS.d/next/Library/2025-06-07-15-10-22.gh-issue-135227.LPeDGt.rst @@ -1,3 +1,3 @@ The :meth:`!add_parser` method of the special action object returned by :meth:`argparse.ArgumentParser.add_subparsers` now uses the *help* value as -the default *description* if none is provided. Patch by Swayam. \ No newline at end of file +the default *description* if none is provided. Patch by Swayam. From 7034e2ea9edc6fd696491d66b7ddb94c7a69bc4f Mon Sep 17 00:00:00 2001 From: Swayam Shah Date: Sat, 7 Jun 2025 21:34:39 +0530 Subject: [PATCH 7/8] add test for changes --- Lib/test/test_argparse.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 08ff41368d9bb0..12cbb78590a63b 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -2934,6 +2934,19 @@ def test_alias_help(self): 3 3 help """)) + def test_help_sets_default_description(self): + parser = argparse.ArgumentParser() + subparsers = parser.add_subparsers(dest="command") + + sp1 = subparsers.add_parser("a", help="help a") + self.assertEqual(sp1.description, "help a") + + sp2 = subparsers.add_parser("b", help="help b", description="explicit desc") + self.assertEqual(sp2.description, "explicit desc") + + sp3 = subparsers.add_parser("c") + self.assertIsNone(sp3.description) + # ============ # Groups tests # ============ From 09de44266f15e1ad63dd168ef5f9ea391f455315 Mon Sep 17 00:00:00 2001 From: Swayam Shah Date: Tue, 10 Jun 2025 20:31:57 +0530 Subject: [PATCH 8/8] restoring behaviour --- Lib/argparse.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index c1196d60ffaf99..a65a84a9a7dac7 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1259,15 +1259,17 @@ def add_parser(self, name, *, deprecated=False, **kwargs): if alias in self._name_parser_map: raise ValueError(f'conflicting subparser alias: {alias}') - # Handle help/description logic - help = kwargs.pop('help', None) - if 'description' not in kwargs and help is not None: - kwargs['description'] = help + help_provided = 'help' in kwargs + help_text = kwargs.pop('help', None) if help_provided else None + + # Set description default ONLY if: + if 'description' not in kwargs and help_text is not None: + kwargs['description'] = help_text # Create the parser and pseudo-action parser = self._parser_class(**kwargs) - if help is not None: - choice_action = self._ChoicesPseudoAction(name, aliases, help) + if help_provided: + choice_action = self._ChoicesPseudoAction(name, aliases, help_text) self._choices_actions.append(choice_action) parser._check_help(choice_action) self._name_parser_map[name] = parser