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.

classification
Title: argparse.BooleanOptionalAction with default=argparse.SUPPRESS and help specified raises exception
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: felixfontein, iritkatriel, miss-islington, paul.j3, taleinat
Priority: normal Keywords: patch

Created on 2021-12-15 06:29 by felixfontein, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30111 merged felixfontein, 2021-12-15 06:35
PR 30730 merged miss-islington, 2022-01-20 22:48
PR 30731 merged miss-islington, 2022-01-20 22:49
Messages (6)
msg408586 - (view) Author: Felix Fontein (felixfontein) * Date: 2021-12-15 06:29
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.)
msg408588 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-15 08:39
Changing type. Crash is typically segfault or hang in c code rather than an exception.
msg411052 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2022-01-20 22:48
New changeset 9e87c0e03fa501fb90008547983ce4c1dcaaf90c by Felix Fontein in branch 'main':
bpo-46080: fix argparse help generation exception in edge case (GH-30111)
https://github.com/python/cpython/commit/9e87c0e03fa501fb90008547983ce4c1dcaaf90c
msg411055 - (view) Author: miss-islington (miss-islington) Date: 2022-01-20 23:13
New changeset e5edc8d737a45d9d8b9b93b8be52f85d79d0f417 by Miss Islington (bot) in branch '3.10':
bpo-46080: fix argparse help generation exception in edge case (GH-30111)
https://github.com/python/cpython/commit/e5edc8d737a45d9d8b9b93b8be52f85d79d0f417
msg411057 - (view) Author: miss-islington (miss-islington) Date: 2022-01-20 23:22
New changeset c6691a7ccbd027298ea2486014b55db037fffc9f by Miss Islington (bot) in branch '3.9':
bpo-46080: fix argparse help generation exception in edge case (GH-30111)
https://github.com/python/cpython/commit/c6691a7ccbd027298ea2486014b55db037fffc9f
msg411096 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2022-01-21 08:20
Thanks for the report and the PR, Felix!
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90238
2022-02-02 16:53:48paul.j3setnosy: + paul.j3
2022-01-21 08:20:51taleinatsetstatus: open -> closed
resolution: fixed
messages: + msg411096

stage: patch review -> resolved
2022-01-20 23:22:57miss-islingtonsetmessages: + msg411057
2022-01-20 23:13:25miss-islingtonsetmessages: + msg411055
2022-01-20 22:49:02miss-islingtonsetpull_requests: + pull_request28918
2022-01-20 22:48:56miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request28917
2022-01-20 22:48:56taleinatsetnosy: + taleinat
messages: + msg411052
2021-12-15 14:59:48iritkatrielsettitle: argparse.BooleanOptionalAction with default=argparse.SUPPRESS and help specified crashes -> argparse.BooleanOptionalAction with default=argparse.SUPPRESS and help specified raises exception
2021-12-15 08:39:51iritkatrielsettype: crash -> behavior

messages: + msg408588
nosy: + iritkatriel
2021-12-15 06:36:04felixfonteinsettype: crash
2021-12-15 06:35:08felixfonteinsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28333
2021-12-15 06:29:31felixfonteincreate