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.

Title: argparse boolean type bug
Type: behavior Stage: resolved
Components: Versions: Python 3.7
Status: closed Resolution: duplicate
Dependencies: Superseder: ArgumentParser should support bool type according to truth values
View: 37564
Assigned To: Nosy List: Trenton Bricken, mark.dickinson, paul.j3, rhettinger, xtreak
Priority: normal Keywords:

Created on 2019-12-30 16:18 by Trenton Bricken, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (7)
msg359045 - (view) Author: Trenton Bricken (Trenton Bricken) Date: 2019-12-30 16:18
This is a bug with argparse. 

Say I have:

parser.add_argument('--verbose', type=bool, action='store', nargs='+',
                        default = [False],
                        help='turns on verbosity')

If in the command line I have "--verbose False' the default will then evaluate to True and return the value "True" rather than the value of "False" that I tried to set it to!

When a developer has lots of arguments to pass in, they may not remember if an argument defaults to False or True. Setting the value to False and having it then return True is very confusing and should not occur. 

Right now I have a work-around where I have a new type=buildBool where:

def buildBool(arg):
   return bool(arg)

and do:

parser.add_argument('--verbose', type=buildBool, action='store', nargs='+',
                        default = ['False'],
                        help='turns on verbosity')

but this means I have to have this type and have my default value be a string which is suboptimal and this bug remains a trap for other developers.
msg359047 - (view) Author: Trenton Bricken (Trenton Bricken) Date: 2019-12-30 16:28

I was being dumb before, the problem still remains but my work around previously was wrong. This is the new workaround: 

def buildBool(arg):
    if arg == 'False':
        return False
        return True
msg359048 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-12-30 16:45
It seems like this is a common problem : . I guess you want to store verbose=True when --verbose is passed and verbose=False when --verbose is not passed where store_true might help.
msg359051 - (view) Author: Trenton Bricken (Trenton Bricken) Date: 2019-12-30 17:28
Thank you for your quick and helpful reply. 

The problem with your solution is twofold: 
1. it adds some cognitive load in needing to remember whether or not the flag defaults to True or False and thus whether or not you need to add it. It is easier for me to have everything as a flag with a value that follows. 
2. More importantly, I am using argparse for trying lots of different combinations of inputs to a function so I pass them in as a list and then permute the possible values for each input. 

For example: --flag1 a b
--flag2 c d
I want to then run a command with the parameter combinations: 
a, c; a, d; b, c; b, d;

And I don't think store_true store_false would allow me to pass in having combinations of True and False like --flag True False would. 

I am fine now with my current work around, but was very confused for some time why my flag would be true when I set it to false. And think this is a counterintuitive pitfall other developers can fall into.
msg359054 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2019-12-30 19:30
Despite the name, the 'type' parameter specifies a function, not a Python class.  

The only string that produces False is the empty one: bool('').  So 'type=bool' is valid Python, even if it isn't useful.

With `nargs='+'` there's no problem with providing strings like 'False', 'true', 'no', 'oui', 'niet', but if you want to convert those to boolean True/False values, you need to write your own 'type' function.

There was a recent bug/issue that proposed providing such a function (or importing it from another module), and shadowing the existing 'bool' function, but that has been rejected (I think).  It isn't really needed, and the proposed solution was too language specific.

Seems to me that expecting your user to provide an open ended list of 'True False False True' strings would be rather confusing, or at least require a complicated 'help' string.  In any case it's not a common enough case to require any changes to the core argparse functionality.

In sum, it isn't clear what the bug is, or what patch you expect.  This sounds more like a StackOverflow question, than a bug report.
msg359058 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2019-12-30 20:48
The rejected boolean type proposal:
msg359113 - (view) Author: Trenton Bricken (Trenton Bricken) Date: 2019-12-31 12:27
Thanks for all of these replies. 

The functionality is that the user adds --flag True False and then all of the other parameters are run with True. And then again with False. 

I thought this was a bug because of the confusing type=bool behavior. But it certainly isn't a big deal and it seems like you are already aware of it. 

Thanks for all of your open source contributions and help.
Date User Action Args
2022-04-11 14:59:24adminsetgithub: 83348
2019-12-31 12:27:25Trenton Brickensetmessages: + msg359113
2019-12-31 01:26:28josh.rsetstatus: open -> closed
superseder: ArgumentParser should support bool type according to truth values
resolution: duplicate
stage: resolved
2019-12-30 21:19:32mark.dickinsonsetmessages: - msg359060
2019-12-30 21:18:35mark.dickinsonsetnosy: + mark.dickinson
messages: + msg359060
2019-12-30 20:48:14paul.j3setmessages: + msg359058
2019-12-30 19:30:54paul.j3setmessages: + msg359054
2019-12-30 17:28:05Trenton Brickensetmessages: + msg359051
2019-12-30 16:45:43xtreaksetnosy: + rhettinger, xtreak, paul.j3
messages: + msg359048
2019-12-30 16:28:13Trenton Brickensetmessages: + msg359047
2019-12-30 16:18:30Trenton Brickencreate