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

For argparse add_argument with action='store_const', const should default to None. #82061

Closed
nanjekyejoannah opened this issue Aug 18, 2019 · 5 comments
Assignees
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@nanjekyejoannah
Copy link
Member

BPO 37880
Nosy @vsajip, @bitdancer, @tyomitch, @nanjekyejoannah, @jacobtylerwalls, @jdevries3133
PRs
  • bpo-37880: for argparse add_argument with action='store_const', const defaults to None. #26707
  • 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 = 'https://github.com/vsajip'
    closed_at = <Date 2021-08-01.06:20:10.974>
    created_at = <Date 2019-08-18.00:41:15.421>
    labels = ['type-bug', 'library', '3.11']
    title = "For argparse add_argument with action='store_const', const should default to None."
    updated_at = <Date 2021-08-01.06:20:10.974>
    user = 'https://github.com/nanjekyejoannah'

    bugs.python.org fields:

    activity = <Date 2021-08-01.06:20:10.974>
    actor = 'vinay.sajip'
    assignee = 'vinay.sajip'
    closed = True
    closed_date = <Date 2021-08-01.06:20:10.974>
    closer = 'vinay.sajip'
    components = ['Library (Lib)']
    creation = <Date 2019-08-18.00:41:15.421>
    creator = 'nanjekyejoannah'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37880
    keywords = ['patch']
    message_count = 5.0
    messages = ['349911', '395699', '395700', '395701', '398646']
    nosy_count = 6.0
    nosy_names = ['vinay.sajip', 'r.david.murray', 'A. Skrobov', 'nanjekyejoannah', 'jacobtylerwalls', 'jack__d']
    pr_nums = ['26707']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37880'
    versions = ['Python 3.11']

    @nanjekyejoannah
    Copy link
    Member Author

    Currently, when parser.add_argument() is given argument with action='store_const' and no const argument , it throws an exception :

        >>> from argparse import ArgumentParser
        >>> parser = ArgumentParser()
        >>> parser.add_argument("--foo", help="foo", action='store_const')
        Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/home/captain/projects/cpython/Lib/argparse.py", line 1350, in add_argument
        action = action_class(**kwargs)
        TypeError: __init__() missing 1 required positional argument: 'const'
        >>>

    Specifying the const argument stops this exception:

    >>> parser.add_argument("--foo", help="foo", action='store_const', const=None)
    _StoreConstAction(option_strings=['--foo'], dest='foo', nargs=0, const=None, default=None, type=None, choices=None, help='foo', metavar=None)

    Originally the docs, said when action was set to 'store_const' const defaulted to None which was not matching with the implementation at the time.

    After this commit : https://github.com/python/cpython/commi/b4912b8ed367e540ee060fe912f841cc764fd293, The docs were updated to match the implementation to fix Bpo issues :

    https://bugs.python.org/issue25299

    https://bugs.python.org/issue24754 and

    https://bugs.python.org/issue25314

    I suggest that we make const default to None If action='store_const' as was intended originally before edits to the docs. If no one objects, I can open a PR for this.

    @nanjekyejoannah nanjekyejoannah added the 3.9 only security fixes label Aug 18, 2019
    @jacobtylerwalls
    Copy link
    Mannequin

    jacobtylerwalls mannequin commented Jun 12, 2021

    Sounds reasonable to me.

    @jacobtylerwalls jacobtylerwalls mannequin added stdlib Python modules in the Lib dir 3.11 only security fixes type-bug An unexpected behavior, bug, or error and removed 3.9 only security fixes labels Jun 12, 2021
    @jdevries3133
    Copy link
    Mannequin

    jdevries3133 mannequin commented Jun 12, 2021

    Hi Joannah, I'm a new contributor and happy to take a crack at this if you haven't already started on a fix / would like me to do that. Thanks!

    @nanjekyejoannah
    Copy link
    Member Author

    @jack__d, please feel free to work on a PR.

    Also, do not hesitate to ask any questions along the way.

    Thanks for contributing to CPython.

    @vsajip
    Copy link
    Member

    vsajip commented Jul 31, 2021

    New changeset 0ad1732 by Jack DeVries in branch 'main':
    bpo-37880: for argparse add_argument with action='store_const', const now defaults to None. (GH-26707)
    0ad1732

    @vsajip vsajip closed this as completed Aug 1, 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.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants