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: For argparse add_argument with action='store_const', const should default to None.
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: A. Skrobov, jack__d, jacobtylerwalls, nanjekyejoannah, r.david.murray, vinay.sajip
Priority: normal Keywords: patch

Created on 2019-08-18 00:41 by nanjekyejoannah, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 26707 merged jack__d, 2021-06-13 15:35
Messages (5)
msg349911 - (view) Author: Joannah Nanjekye (nanjekyejoannah) * (Python committer) Date: 2019-08-18 00:41
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/", 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 :, The docs were updated to match the implementation to fix Bpo issues : and

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.
msg395699 - (view) Author: Jacob Walls (jacobtylerwalls) * Date: 2021-06-12 19:00
Sounds reasonable to me.
msg395700 - (view) Author: Jack DeVries (jack__d) * Date: 2021-06-12 19:45
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!
msg395701 - (view) Author: Joannah Nanjekye (nanjekyejoannah) * (Python committer) Date: 2021-06-12 19:49
@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.
msg398646 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2021-07-31 16:28
New changeset 0ad173249d287794d53e6a1fe2d58bb2adee2276 by Jack DeVries in branch 'main':
bpo-37880: for argparse add_argument with action='store_const', const now defaults to None. (GH-26707)
Date User Action Args
2022-04-11 14:59:19adminsetgithub: 82061
2021-08-01 06:20:10vinay.sajipsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-07-31 16:28:03vinay.sajipsetmessages: + msg398646
2021-06-15 04:33:25rhettingersetassignee: vinay.sajip

nosy: + vinay.sajip
2021-06-13 15:35:00jack__dsetkeywords: + patch
stage: patch review
pull_requests: + pull_request25294
2021-06-12 19:49:47nanjekyejoannahsetmessages: + msg395701
2021-06-12 19:45:06jack__dsetnosy: + jack__d
messages: + msg395700
2021-06-12 19:00:41jacobtylerwallssetversions: + Python 3.11, - Python 3.9
nosy: + jacobtylerwalls

messages: + msg395699

components: + Library (Lib)
type: behavior
2019-08-18 00:41:15nanjekyejoannahcreate