[New-bugs-announce] [issue39167] argparse boolean type bug

Trenton Bricken report at bugs.python.org
Mon Dec 30 11:18:30 EST 2019

New submission from Trenton Bricken <brickentrenton at gmail.com>:

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.

messages: 359045
nosy: Trenton Bricken
priority: normal
severity: normal
status: open
title: argparse boolean type bug
type: behavior
versions: Python 3.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list