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 paul.j3
Recipients Zach Beniash, paul.j3
Date 2019-07-11.20:06:48
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1562875609.46.0.657282581338.issue37564@roundup.psfhosted.org>
In-reply-to
Content
This is an old topic, though it may be easier to find a relevant StackOverflow thread.

https://stackoverflow.com/a/19233287/901925  (a 2013 thread)

I and others have explained that the `type` parameter is supposed to be a callable.  The default Python `bool` function only returns False for an empty string.  Previous discussions have suggested writing a function that parses strings like 'False' and 'No'.  It's nice that you have found a 'distutils.utils.strtobool' that does this well.

But I have several qualms about the proposed patch:

- do we need to define a 'bool' type?  We already has 'store_true' and 'store_false' actions.

- does this function need a 'bool' wrapper?

- do we need another import?  There's a relatively recent bug/issue that tries to speed up argparse by reducing the number of imports.

- if added, should this type be registered as the object bool or string 'bool'?  

Seems to me that the patch could be simplified to:

     self.register('type', 'bool', _strtobool)

According the original developer `type=bool` should be allowed (even if it doesn't do what many users expect):

https://bugs.python.org/issue14392

I point out that users are used to providing strings for 'action', but not for 'type'.  The registry mechanism can be used to provide specialized type but so far only 'None' is registered.

https://bugs.python.org/issue26994

https://bugs.python.org/issue24754

This proposed patch might be innocuous, but it does break a consistent behavior - that 'type' should be a callable, and 'bool' is a standard Python function.

An alternative is to add to the docs a note (in the 'type' section) that

    from distutils.util import strtobool
    type=strtobool

could be used to parse strings as booleans.
History
Date User Action Args
2019-07-11 20:06:49paul.j3setrecipients: + paul.j3, Zach Beniash
2019-07-11 20:06:49paul.j3setmessageid: <1562875609.46.0.657282581338.issue37564@roundup.psfhosted.org>
2019-07-11 20:06:49paul.j3linkissue37564 messages
2019-07-11 20:06:48paul.j3create