Skip to content

bpo-46080: Fix crash when argparse.BooleanOptionalAction is used with default=argparse.SUPPRESS and help specified #30111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down
7 changes: 5 additions & 2 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'), [
Expand All @@ -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 + '''\
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.