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.

Title: In argparse empty choices cannot be printed in the help
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: andrei.avk, lukasz.langa, miss-islington, paul.j3, py.user
Priority: normal Keywords: patch

Created on 2015-06-13 06:47 by py.user, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28050 merged andrei.avk, 2021-08-30 04:29
PR 28931 merged miss-islington, 2021-10-13 16:32
PR 28932 merged miss-islington, 2021-10-13 16:32
Messages (7)
msg245297 - (view) Author: py.user (py.user) * Date: 2015-06-13 06:47
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> _ = parser.add_argument('foo', choices=[], help='%(choices)s')
>>> parser.print_help()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/guest/tmp/tests/misc/git/example/cpython/main/Lib/", line 2358, in print_help
    self._print_message(self.format_help(), file)
  File "/home/guest/tmp/tests/misc/git/example/cpython/main/Lib/", line 2342, in format_help
    return formatter.format_help()
  File "/home/guest/tmp/tests/misc/git/example/cpython/main/Lib/", line 278, in format_help
    help = self._root_section.format_help()
  File "/home/guest/tmp/tests/misc/git/example/cpython/main/Lib/", line 208, in format_help
  File "/home/guest/tmp/tests/misc/git/example/cpython/main/Lib/", line 208, in format_help
  File "/home/guest/tmp/tests/misc/git/example/cpython/main/Lib/", line 517, in _format_action
    parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
IndexError: list index out of range

It's not very useful to print empty choices, but the choices list could be formed dynamically. So the command-line user can't figure out what's happen.
msg245448 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-06-18 01:07
Does this work for

    help='choices: %(choices)s'

Without setting up my own test case, I'm guessing that the problem is with an empty help string.  `help=''` should be ok, but the test handle that probably occurs earlier.

Choices, empty or not, are included earlier in the help line, as well as usage.  So there isn't real need to include them in the user defined part of the help line.

Still, there are other bug issues regarding the formatting of choices.  That part needs refinement to handle very long lists of choices, as well as ones that don't fit the usual 'list' format.
msg273317 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2016-08-21 20:45
This error is also produced by

    help = ' '

that is, a help line with all blanks.  As long as there is a nonblank character in the help line it format fine.

This issue doesn't need to be resolved by itself, but should be considered if and when the formatting of choices is reworked.
msg403849 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-13 16:32
New changeset 6fafc25aea8689048314b5bf7a9bb986bb1ce238 by andrei kulakov in branch 'main':
bpo-24444: fix an error in argparse help when help for an option is blank (GH-28050)
msg403858 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-13 17:15
New changeset fd2be6da2ffec2301a58aefa92f17f0c567fbc55 by Miss Islington (bot) in branch '3.10':
bpo-24444: fix an error in argparse help when help for an option is blank (GH-28050) (GH-28931)
msg403871 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-13 20:36
New changeset fb7203453855a43907048ff2b8f9b890305b3d15 by Miss Islington (bot) in branch '3.9':
[3.9] bpo-24444: fix an error in argparse help when help for an option is blank (GH-28050) (GH-28932)
msg403872 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-10-13 20:37
Thanks, Andrei! ✨ 🍰 ✨
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68632
2021-10-13 20:37:35lukasz.langasetstatus: open -> closed
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.6
messages: + msg403872

resolution: fixed
stage: patch review -> resolved
2021-10-13 20:36:20lukasz.langasetmessages: + msg403871
2021-10-13 17:15:47lukasz.langasetmessages: + msg403858
2021-10-13 16:32:12miss-islingtonsetpull_requests: + pull_request27221
2021-10-13 16:32:05lukasz.langasetnosy: + lukasz.langa
messages: + msg403849
2021-10-13 16:32:04miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request27220
2021-09-09 17:48:46iritkatriellinkissue39955 superseder
2021-08-30 04:29:38andrei.avksetkeywords: + patch
nosy: + andrei.avk

pull_requests: + pull_request26494
stage: patch review
2016-08-21 20:45:35paul.j3setmessages: + msg273317
2015-06-18 01:07:22paul.j3setnosy: + paul.j3
messages: + msg245448
2015-06-13 06:47:02py.usercreate