classification
Title: argparse.BooleanOptionalAction should not add the default value to the help string by default
Type: Stage: patch review
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Antony.Lee, Micky Yun Chan, paul.j3, rhettinger
Priority: normal Keywords: patch

Created on 2019-12-02 23:03 by Antony.Lee, last changed 2019-12-03 07:11 by Micky Yun Chan.

Pull Requests
URL Status Linked Edit
PR 17447 open Micky Yun Chan, 2019-12-03 07:05
Messages (3)
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
History
Date User Action Args
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