[Python-checkins] bpo-39716: Raise on conflicting subparser names. (GH-18605)

miss-islington webhook-mailer at python.org
Sun May 1 02:04:54 EDT 2022


https://github.com/python/cpython/commit/ad5e8520f3e117f45481513014548a39879d30d2
commit: ad5e8520f3e117f45481513014548a39879d30d2
branch: main
author: Antony Lee <anntzer.lee at gmail.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-04-30T23:04:50-07:00
summary:

bpo-39716: Raise on conflicting subparser names. (GH-18605)



Raise an ArgumentError when the same subparser name is added twice to an
ArgumentParser.  This is consistent with the (default) behavior when the
same option string is added twice to an ArgumentParser.

(Support for `conflict_handler="resolve"` could be considered as a
followup feature, although real use cases seem even rarer than
"resolve"ing option-strings.)

Automerge-Triggered-By: GH:rhettinger

files:
A Misc/NEWS.d/next/Library/2020-02-22-12-02-11.bpo-39716.z2WhDQ.rst
M Lib/argparse.py
M Lib/test/test_argparse.py

diff --git a/Lib/argparse.py b/Lib/argparse.py
index 668e1d416231f..c47aeffc2c3f6 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1171,6 +1171,13 @@ def add_parser(self, name, **kwargs):
 
         aliases = kwargs.pop('aliases', ())
 
+        if name in self._name_parser_map:
+            raise ArgumentError(self, _('conflicting subparser: %s') % name)
+        for alias in aliases:
+            if alias in self._name_parser_map:
+                raise ArgumentError(
+                    self, _('conflicting subparser alias: %s') % alias)
+
         # create a pseudo-action to hold the choice help
         if 'help' in kwargs:
             help = kwargs.pop('help')
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 5777cb50f7e53..794b9df3796df 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -4804,6 +4804,19 @@ def test_resolve_error(self):
               --spam NEW_SPAM
             '''))
 
+    def test_subparser_conflict(self):
+        parser = argparse.ArgumentParser()
+        sp = parser.add_subparsers()
+        sp.add_parser('fullname', aliases=['alias'])
+        self.assertRaises(argparse.ArgumentError,
+                          sp.add_parser, 'fullname')
+        self.assertRaises(argparse.ArgumentError,
+                          sp.add_parser, 'alias')
+        self.assertRaises(argparse.ArgumentError,
+                          sp.add_parser, 'other', aliases=['fullname'])
+        self.assertRaises(argparse.ArgumentError,
+                          sp.add_parser, 'other', aliases=['alias'])
+
 
 # =============================
 # Help and Version option tests
diff --git a/Misc/NEWS.d/next/Library/2020-02-22-12-02-11.bpo-39716.z2WhDQ.rst b/Misc/NEWS.d/next/Library/2020-02-22-12-02-11.bpo-39716.z2WhDQ.rst
new file mode 100644
index 0000000000000..f122811e6bfe3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-02-22-12-02-11.bpo-39716.z2WhDQ.rst
@@ -0,0 +1,3 @@
+Raise an ArgumentError when the same subparser name is added twice to an
+`argparse.ArgumentParser`.  This is consistent with the (default) behavior
+when the same option string is added twice to an ArgumentParser.



More information about the Python-checkins mailing list