This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author felixfontein
Recipients felixfontein
Date 2021-12-15.06:29:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1639549771.77.0.946249381009.issue46080@roundup.psfhosted.org>
In-reply-to
Content
When argparse.BooleanOptionalAction is used with default=argparse.SUPPRESS and help is specified, trying to display --help results in a crash.

Reproducer:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--test', action=argparse.BooleanOptionalAction, default=argparse.SUPPRESS, help='Foo')
parser.parse_args()

Result when running 'python t.py --help':

Traceback (most recent call last):
  File "/path/to/t.py", line 4, in <module>
    parser.parse_args()
  File "/usr/lib/python3.10/argparse.py", line 1821, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/usr/lib/python3.10/argparse.py", line 1854, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/usr/lib/python3.10/argparse.py", line 2063, in _parse_known_args
    start_index = consume_optional(start_index)
  File "/usr/lib/python3.10/argparse.py", line 2003, in consume_optional
    take_action(action, args, option_string)
  File "/usr/lib/python3.10/argparse.py", line 1931, in take_action
    action(self, namespace, argument_values, option_string)
  File "/usr/lib/python3.10/argparse.py", line 1095, in __call__
    parser.print_help()
  File "/usr/lib/python3.10/argparse.py", line 2551, in print_help
    self._print_message(self.format_help(), file)
  File "/usr/lib/python3.10/argparse.py", line 2535, in format_help
    return formatter.format_help()
  File "/usr/lib/python3.10/argparse.py", line 283, in format_help
    help = self._root_section.format_help()
  File "/usr/lib/python3.10/argparse.py", line 214, in format_help
    item_help = join([func(*args) for func, args in self.items])
  File "/usr/lib/python3.10/argparse.py", line 214, in <listcomp>
    item_help = join([func(*args) for func, args in self.items])
  File "/usr/lib/python3.10/argparse.py", line 214, in format_help
    item_help = join([func(*args) for func, args in self.items])
  File "/usr/lib/python3.10/argparse.py", line 214, in <listcomp>
    item_help = join([func(*args) for func, args in self.items])
  File "/usr/lib/python3.10/argparse.py", line 530, in _format_action
    help_text = self._expand_help(action)
  File "/usr/lib/python3.10/argparse.py", line 627, in _expand_help
    return self._get_help_string(action) % params
KeyError: 'default'

The problem is that in https://github.com/python/cpython/blob/f54fee7f37563fbd569596cf94aad023ac6c3179/Lib/argparse.py#L879, default isn't tested for SUPPRESS as well. I've prepared a patch to fix that and will push it to GitHub soon.

(I've experienced the crash with Python 3.9 and 3.10; the test I've created also crashes for the current main branch, so I guess that means that Python 3.11 is also affected. I haven't tested older Python versions before 3.9.)
History
Date User Action Args
2021-12-15 06:29:31felixfonteinsetrecipients: + felixfontein
2021-12-15 06:29:31felixfonteinsetmessageid: <1639549771.77.0.946249381009.issue46080@roundup.psfhosted.org>
2021-12-15 06:29:31felixfonteinlinkissue46080 messages
2021-12-15 06:29:31felixfonteincreate