Title: For argparse add_argument with action='store_const', const should default to None.
Type: Stage:
Components: Versions: Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: A. Skrobov, nanjekyejoannah, r.david.murray
Priority: normal Keywords:

Created on 2019-08-18 00:41 by nanjekyejoannah, last changed 2019-08-18 00:41 by nanjekyejoannah.

Messages (1)
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.
Date User Action Args
2019-08-18 00:41:15nanjekyejoannahcreate