]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46080: fix argparse help generation exception in edge case (GH-30111)
authorFelix Fontein <felix@fontein.de>
Thu, 20 Jan 2022 22:48:48 +0000 (23:48 +0100)
committerGitHub <noreply@github.com>
Thu, 20 Jan 2022 22:48:48 +0000 (00:48 +0200)
Fix an uncaught exception during help text generation when
argparse.BooleanOptionalAction is used with default=argparse.SUPPRESS
and help is specified.

Lib/argparse.py
Lib/test/test_argparse.py
Misc/NEWS.d/next/Library/2021-12-15-06-29-00.bpo-46080.AuQpLt.rst [new file with mode: 0644]

index 1529d9e76873739edade94cbf8daa7b1004f8f8e..9344dab3e60d5a24beda9d1739633cda80c2c960 100644 (file)
@@ -881,7 +881,7 @@ class BooleanOptionalAction(Action):
                 option_string = '--no-' + option_string[2:]
                 _option_strings.append(option_string)
 
-        if help is not None and default is not None:
+        if help is not None and default is not None and default is not SUPPRESS:
             help += " (default: %(default)s)"
 
         super().__init__(
index afcb88ff5ce0f8bf7a2ba051bcb6cdb8e433493b..df6da928c9bebf27a449f0674e86aef855fd5510 100644 (file)
@@ -3626,6 +3626,8 @@ class TestHelpUsage(HelpTestCase):
         Sig('--bar', help='Whether to bar', default=True,
                      action=argparse.BooleanOptionalAction),
         Sig('-f', '--foobar', '--barfoo', action=argparse.BooleanOptionalAction),
+        Sig('--bazz', action=argparse.BooleanOptionalAction,
+                      default=argparse.SUPPRESS, help='Bazz!'),
     ]
     argument_group_signatures = [
         (Sig('group'), [
@@ -3638,8 +3640,8 @@ class TestHelpUsage(HelpTestCase):
     usage = '''\
         usage: PROG [-h] [-w W [W ...]] [-x [X ...]] [--foo | --no-foo]
                     [--bar | --no-bar]
-                    [-f | --foobar | --no-foobar | --barfoo | --no-barfoo] [-y [Y]]
-                    [-z Z Z Z]
+                    [-f | --foobar | --no-foobar | --barfoo | --no-barfoo]
+                    [--bazz | --no-bazz] [-y [Y]] [-z Z Z Z]
                     a b b [c] [d ...] e [e ...]
         '''
     help = usage + '''\
@@ -3656,6 +3658,7 @@ class TestHelpUsage(HelpTestCase):
           --foo, --no-foo       Whether to foo
           --bar, --no-bar       Whether to bar (default: True)
           -f, --foobar, --no-foobar, --barfoo, --no-barfoo
+          --bazz, --no-bazz     Bazz!
 
         group:
           -y [Y]                y
diff --git a/Misc/NEWS.d/next/Library/2021-12-15-06-29-00.bpo-46080.AuQpLt.rst b/Misc/NEWS.d/next/Library/2021-12-15-06-29-00.bpo-46080.AuQpLt.rst
new file mode 100644 (file)
index 0000000..e42d84e
--- /dev/null
@@ -0,0 +1,3 @@
+Fix exception in argparse help text generation if a
+:class:`argparse.BooleanOptionalAction` argument's default is
+``argparse.SUPPRESS`` and it has ``help`` specified.  Patch by Felix Fontein.
\ No newline at end of file