classification
Title: argparse.BooleanOptionalAction should not add the default value to the help string by default
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Micky Yun Chan, lukasz.langa, mhils, miss-islington, paul.j3, rhettinger, shihai1991
Priority: normal Keywords: patch

Created on 2019-12-02 23:03 by Antony.Lee, last changed 2021-08-17 19:16 by Antony.Lee. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 17447 closed Micky Yun Chan, 2019-12-03 07:05
PR 27672 merged mhils, 2021-08-09 08:46
PR 27787 merged miss-islington, 2021-08-16 21:42
PR 27788 merged lukasz.langa, 2021-08-16 21:50
Messages (7)
msg357733 - (view) Author: Antony Lee (Antony.Lee) * Date: 2019-12-02 23:03
https://bugs.python.org/issue8538 recently added to Py3.9 a much welcome addition to argparse, namely the capability to generate --foo/--no-foo flag pairs.  A small issue with the implementation is that it *always* appends the default value to the help string (if any):

    if help is not None and default is not None:
        help += f" (default: {default})"

This is inconsistent with other action classes, and results in the defaults being printed twice if using ArgumentsDefaultHelpFormatter (which is the documented way to include the defaults in the help text):

    from argparse import *
    parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument("--foo", action=BooleanOptionalAction, help="Whether to foo it", default=True)
    parser.add_argument("--quux", help="Set the quux", default=42)
    print(parser.parse_args())

yields

    usage: foo.py [-h] [--foo | --no-foo] [--quux QUUX]

    optional arguments:
      -h, --help       show this help message and exit
      --foo, --no-foo  Whether to foo it (default: True) (default: True)  # <--- HERE
      --quux QUUX      Set the quux (default: 42)

I think the fix is just a matter of not adding the default value to the help string.
msg357743 - (view) Author: Micky Yun Chan (Micky Yun Chan) * Date: 2019-12-03 06:14
Hi, if it's possible can I take this issue?
msg357747 - (view) Author: Micky Yun Chan (Micky Yun Chan) * Date: 2019-12-03 07:11
Hi I created a new pull request on Github
msg399687 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-16 21:42
New changeset 1512bc21d60f098a9e9f37b44a2f6a9b49a3fd4f by Maximilian Hils in branch 'main':
bpo-38956: don't print BooleanOptionalAction's default twice (GH-27672)
https://github.com/python/cpython/commit/1512bc21d60f098a9e9f37b44a2f6a9b49a3fd4f
msg399720 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-17 09:17
New changeset 27fd31311097ab7326f084b5e0e5f388f8dc6b13 by Łukasz Langa in branch '3.9':
[3.9] bpo-38956: don't print BooleanOptionalAction's default twice (GH-27672) (GH-27788)
https://github.com/python/cpython/commit/27fd31311097ab7326f084b5e0e5f388f8dc6b13
msg399724 - (view) Author: miss-islington (miss-islington) Date: 2021-08-17 09:40
New changeset 6f6648e436d02bce0e49ba82f4377c0d2f586f0f by Miss Islington (bot) in branch '3.10':
bpo-38956: don't print BooleanOptionalAction's default twice (GH-27672)
https://github.com/python/cpython/commit/6f6648e436d02bce0e49ba82f4377c0d2f586f0f
msg399729 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-17 10:03
Thanks, Max for pushing this across the finish line! ✨ 🍰 ✨  

Closing this one. Further improvement of BooleanOptionalAction will be discussed in BPO-44587.
History
Date User Action Args
2021-08-17 19:16:12Antony.Leesetnosy: - Antony.Lee
2021-08-17 10:03:35lukasz.langasetstatus: open -> closed
resolution: fixed
messages: + msg399729

stage: patch review -> resolved
2021-08-17 09:40:49miss-islingtonsetmessages: + msg399724
2021-08-17 09:17:10lukasz.langasetmessages: + msg399720
2021-08-16 21:50:17lukasz.langasetpull_requests: + pull_request26257
2021-08-16 21:42:43miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26256
2021-08-16 21:42:33lukasz.langasetnosy: + lukasz.langa
messages: + msg399687
2021-08-09 08:46:38mhilssetnosy: + mhils
pull_requests: + pull_request26160
2019-12-09 15:53:17shihai1991setnosy: + shihai1991
2019-12-03 07:11:44Micky Yun Chansetmessages: + msg357747
2019-12-03 07:05:11Micky Yun Chansetkeywords: + patch
stage: patch review
pull_requests: + pull_request16928
2019-12-03 06:14:57Micky Yun Chansetnosy: + Micky Yun Chan
messages: + msg357743
2019-12-03 03:01:53xtreaksetnosy: + rhettinger, paul.j3
2019-12-02 23:03:24Antony.Leecreate