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.

Author nanjekyejoannah
Recipients A. Skrobov, nanjekyejoannah, r.david.murray
Date 2019-08-18.00:41:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1566088875.45.0.199116489639.issue37880@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2019-08-18 00:41:15nanjekyejoannahsetrecipients: + nanjekyejoannah, r.david.murray, A. Skrobov
2019-08-18 00:41:15nanjekyejoannahsetmessageid: <1566088875.45.0.199116489639.issue37880@roundup.psfhosted.org>
2019-08-18 00:41:15nanjekyejoannahlinkissue37880 messages
2019-08-18 00:41:15nanjekyejoannahcreate