From 4f34667a7c3e98add7fc9446cab52ea7488070e0 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Wed, 15 Dec 2021 07:28:55 +0100 Subject: [PATCH 1/3] bpo-46080: Fix crash when argparse.BooleanOptionalAction is used with default=argparse.SUPPRESS and help specified. --- Lib/argparse.py | 2 +- Lib/test/test_argparse.py | 7 +++++-- .../next/Library/2021-12-15-06-29-00.bpo-46080.AuQpLt.rst | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2021-12-15-06-29-00.bpo-46080.AuQpLt.rst diff --git a/Lib/argparse.py b/Lib/argparse.py index b44fa4f0f65c39..f6c3c58d4587fb 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -876,7 +876,7 @@ def __init__(self, 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 f3edde3de8eefb..f8040e3e8c3261 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -3611,6 +3611,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'), [ @@ -3623,8 +3625,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 + '''\ @@ -3641,6 +3643,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 00000000000000..4249e46b8881e8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-12-15-06-29-00.bpo-46080.AuQpLt.rst @@ -0,0 +1 @@ +No longer crashes on help text composition if :class:`argparse.BooleanOptionalAction`'s default is ``argparse.SUPPRESS`` and ``help`` is specified. \ No newline at end of file From b86948b537ac1731f1d9e271ad3a2300061f4612 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 8 Jan 2022 21:40:30 +0100 Subject: [PATCH 2/3] Update Misc/NEWS.d/next/Library/2021-12-15-06-29-00.bpo-46080.AuQpLt.rst Co-authored-by: Tal Einat <532281+taleinat@users.noreply.github.com> --- .../next/Library/2021-12-15-06-29-00.bpo-46080.AuQpLt.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 index 4249e46b8881e8..8c6f8e0b2cc2bb 100644 --- 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 @@ -1 +1,3 @@ -No longer crashes on help text composition if :class:`argparse.BooleanOptionalAction`'s default is ``argparse.SUPPRESS`` and ``help`` is specified. \ No newline at end of file +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 Fontain. \ No newline at end of file From be19c30654be32621690b575c41acb3c6c44dcbe Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 8 Jan 2022 21:41:05 +0100 Subject: [PATCH 3/3] Fix. --- .../next/Library/2021-12-15-06-29-00.bpo-46080.AuQpLt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 8c6f8e0b2cc2bb..e42d84e31e759f 100644 --- 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 @@ -1,3 +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 Fontain. \ No newline at end of file +``argparse.SUPPRESS`` and it has ``help`` specified. Patch by Felix Fontein. \ No newline at end of file