ImageImage

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.

classification
Title: argparse.BooleanOptionalAction accept and silently discard its the const argument
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: miss-islington, paul.j3, remi.lapeyre, rhettinger
Priority: normal Keywords: patch

Created on 2020-06-04 11:59 by remi.lapeyre, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 20623 merged remi.lapeyre, 2020-06-04 12:06
PR 20664 merged miss-islington, 2020-06-05 22:01
Messages (6)
msg370703 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-06-04 11:59
The action is used to store None, True or False when an argument like --foo or --no-foo is given to the cli so it has no used for this action, but it is accepted without warning:

Python 3.10.0a0 (heads/bpo-wip:6e23a9c82b, Jun  4 2020, 13:41:35) 
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', const='this_is_not_used', action=argparse.BooleanOptionalAction)
BooleanOptionalAction(option_strings=['--foo', '--no-foo'], dest='foo', nargs=0, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> args = parser.parse_args()
>>> args
Namespace(foo=None)



We could either always refuse this argument, or accepted and raise ValueError if it is different than None. The attached PR does the first.
msg370772 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-06-05 17:36
ISTM this should be addressed through documentation rather than by breaking currently deployed code that may have specified a default that already matches what the action does.
msg370776 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-06-05 17:47
argparse.BooleanOptionalAction was introduced in Python3.9 so there is no code there should be no code that already rely on it.
msg370777 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-06-05 18:00
Right.  Go ahead :-)
msg370792 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-06-05 22:00
New changeset b084d1b97e369293d2d2bc0791e2135822c923a8 by Rémi Lapeyre in branch 'master':
bpo-40862: Raise TypeError when const is given to argparse.BooleanOptionalAction (GH-20623)
https://github.com/python/cpython/commit/b084d1b97e369293d2d2bc0791e2135822c923a8
msg370811 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-06-06 02:31
New changeset d5e7348e4105d1d4a1c5bd1087f61041532ecbf3 by Miss Islington (bot) in branch '3.9':
bpo-40862: Raise TypeError when const is given to argparse.BooleanOptionalAction (GH-20623) (GH-20664)
https://github.com/python/cpython/commit/d5e7348e4105d1d4a1c5bd1087f61041532ecbf3
History
Date User Action Args
2022-04-11 14:59:32adminsetgithub: 85039
2020-06-07 16:22:59paul.j3setnosy: + paul.j3
2020-06-06 02:31:45rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-06-06 02:31:27rhettingersetmessages: + msg370811
2020-06-05 22:01:01miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request19881
2020-06-05 22:00:48rhettingersetmessages: + msg370792
2020-06-05 18:00:43rhettingersetmessages: + msg370777
2020-06-05 17:47:55remi.lapeyresetmessages: + msg370776
2020-06-05 17:36:40rhettingersetnosy: + rhettinger
messages: + msg370772
2020-06-04 12:24:58remi.lapeyresettitle: argparse.BooleanOptionalAction accept and silently its the const argument -> argparse.BooleanOptionalAction accept and silently discard its the const argument
2020-06-04 12:06:08remi.lapeyresetkeywords: + patch
stage: patch review
pull_requests: + pull_request19848
2020-06-04 11:59:48remi.lapeyrecreate