classification
Title: type=bool doesn't raise error in argparse.Action
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: bethard, paul.j3, shima__shima, terry.reedy
Priority: normal Keywords: patch

Created on 2012-03-23 07:03 by shima__shima, last changed 2017-01-10 00:45 by paul.j3. This issue is now closed.

Files
File name Uploaded Description Edit
argparse.py.patch shima__shima, 2012-03-23 07:03
Messages (6)
msg156636 - (view) Author: Toshihiro Kamishima (shima__shima) Date: 2012-03-23 07:03
According to the documentation of "argparse.Action", a keyword 'bool' is not allowed for type argument, but it doesn't raise no errors.

One possible way to fix this issue is to check in the "argparse._ActionsContainer.add_argument" method.
msg156728 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-03-25 00:56
This change maked the error message wrong if bool is rejected.
msg156729 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-03-25 00:59
The corresponding current code in 3.2 and 3.3 is the same.
msg156745 - (view) Author: Toshihiro Kamishima (shima__shima) Date: 2012-03-25 09:05
Thank you for your response.

My patch makes raise error by minimal change, but it may be better to raise NameError as in a case specifying "type=string" in 2.7.2.

Similar to the case "type=bool", specifications, such as "type=unicode" or "type=long", don't raise errors.
Because the set of problematic keywords are different between 2.7.x and 3.3.x, I have no idea to completely fix this problem.
msg166062 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2012-07-21 20:19
I can't find anywhere in the documentation where type=bool, type=unicode or type=long are disallowed. They shouldn't be disallowed. If you want to pass type=bool, argparse should not stop you. And it currently doesn't:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('foo', type=bool)
>>> parser.parse_args([''])
Namespace(foo=False)
>>> parser.parse_args(['x'])
Namespace(foo=True)
msg166093 - (view) Author: Toshihiro Kamishima (shima__shima) Date: 2012-07-22 00:10
>     - type -- The type which the command-line arguments should be converted
>         to, should be one of 'string', 'int', 'float', 'complex' or a
>         callable object that accepts a single string argument. If None,
>         'string' is assumed.

I misunderstood this sentence in the docstring of arcparse.Action because `bool` or `unicode` is not appeared in this list, which are callables.

So, I close this issue.
History
Date User Action Args
2017-01-10 00:45:51paul.j3setnosy: + paul.j3
2012-07-22 00:10:21shima__shimasetstatus: open -> closed
resolution: rejected
messages: + msg166093
2012-07-21 20:19:39bethardsetmessages: + msg166062
2012-03-25 09:05:40shima__shimasetmessages: + msg156745
2012-03-25 00:59:03terry.reedysetmessages: + msg156729
versions: + Python 3.2, Python 3.3
2012-03-25 00:56:05terry.reedysetnosy: + terry.reedy, bethard
messages: + msg156728

type: behavior
stage: test needed
2012-03-23 07:03:28shima__shimacreate