diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-01-20 23:13:17 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-20 23:13:17 (GMT) |
commit | e5edc8d737a45d9d8b9b93b8be52f85d79d0f417 (patch) | |
tree | 8448950dc9ab2057fdd805e4f821d9ea09104030 | |
parent | 876ade1ae3a805b546a211fd7303253c10395569 (diff) | |
download | cpython-e5edc8d737a45d9d8b9b93b8be52f85d79d0f417.zip cpython-e5edc8d737a45d9d8b9b93b8be52f85d79d0f417.tar.gz cpython-e5edc8d737a45d9d8b9b93b8be52f85d79d0f417.tar.bz2 |
bpo-46080: fix argparse help generation exception in edge case (GH-30111)
Fix an uncaught exception during help text generation when
argparse.BooleanOptionalAction is used with default=argparse.SUPPRESS
and help is specified.
(cherry picked from commit 9e87c0e03fa501fb90008547983ce4c1dcaaf90c)
Co-authored-by: Felix Fontein <felix@fontein.de>
-rw-r--r-- | Lib/argparse.py | 2 | ||||
-rw-r--r-- | Lib/test/test_argparse.py | 7 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2021-12-15-06-29-00.bpo-46080.AuQpLt.rst | 3 |
3 files changed, 9 insertions, 3 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index e177e4f..b71a670 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -878,7 +878,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__( diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 37a73e0..9d66ace 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -3597,6 +3597,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'), [ @@ -3609,8 +3611,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 + '''\ @@ -3627,6 +3629,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 index 0000000..e42d84e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-12-15-06-29-00.bpo-46080.AuQpLt.rst @@ -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 |