Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparse.BooleanOptionalAction should not add the default value to the help string by default #83137

Closed
anntzer mannequin opened this issue Dec 2, 2019 · 7 comments
Closed
Labels
3.9 only security fixes stdlib Python modules in the Lib dir

Comments

@anntzer
Copy link
Mannequin

anntzer mannequin commented Dec 2, 2019

BPO 38956
Nosy @rhettinger, @ambv, @mhils, @miss-islington, @shihai1991, @michiboo
PRs
  • bpo-38956: remove default option from help string in argparse #17447
  • bpo-38956: don't print BooleanOptionalAction's default twice #27672
  • [3.10] bpo-38956: don't print BooleanOptionalAction's default twice (GH-27672) #27787
  • [3.9] bpo-38956: don't print BooleanOptionalAction's default twice (GH-27672) #27788
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-08-17.10:03:35.321>
    created_at = <Date 2019-12-02.23:03:24.687>
    labels = ['library', '3.9']
    title = 'argparse.BooleanOptionalAction should not add the default value to the help string by default'
    updated_at = <Date 2021-08-17.19:16:12.710>
    user = 'https://github.com/anntzer'

    bugs.python.org fields:

    activity = <Date 2021-08-17.19:16:12.710>
    actor = 'Antony.Lee'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-08-17.10:03:35.321>
    closer = 'lukasz.langa'
    components = ['Library (Lib)']
    creation = <Date 2019-12-02.23:03:24.687>
    creator = 'Antony.Lee'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38956
    keywords = ['patch']
    message_count = 7.0
    messages = ['357733', '357743', '357747', '399687', '399720', '399724', '399729']
    nosy_count = 7.0
    nosy_names = ['rhettinger', 'lukasz.langa', 'paul.j3', 'mhils', 'miss-islington', 'shihai1991', 'Micky Yun Chan']
    pr_nums = ['17447', '27672', '27787', '27788']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue38956'
    versions = ['Python 3.9']

    @anntzer
    Copy link
    Mannequin Author

    anntzer mannequin commented Dec 2, 2019

    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.

    @anntzer anntzer mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir labels Dec 2, 2019
    @michiboo
    Copy link
    Mannequin

    michiboo mannequin commented Dec 3, 2019

    Hi, if it's possible can I take this issue?

    @michiboo
    Copy link
    Mannequin

    michiboo mannequin commented Dec 3, 2019

    Hi I created a new pull request on Github

    @ambv
    Copy link
    Contributor

    ambv commented Aug 16, 2021

    New changeset 1512bc2 by Maximilian Hils in branch 'main':
    bpo-38956: don't print BooleanOptionalAction's default twice (GH-27672)
    1512bc2

    @ambv
    Copy link
    Contributor

    ambv commented Aug 17, 2021

    New changeset 27fd313 by Łukasz Langa in branch '3.9':
    [3.9] bpo-38956: don't print BooleanOptionalAction's default twice (GH-27672) (GH-27788)
    27fd313

    @miss-islington
    Copy link
    Contributor

    New changeset 6f6648e by Miss Islington (bot) in branch '3.10':
    bpo-38956: don't print BooleanOptionalAction's default twice (GH-27672)
    6f6648e

    @ambv
    Copy link
    Contributor

    ambv commented Aug 17, 2021

    Thanks, Max for pushing this across the finish line! ✨ 🍰 ✨

    Closing this one. Further improvement of BooleanOptionalAction will be discussed in BPO-44587.

    @ambv ambv closed this as completed Aug 17, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants