-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
argparse boolean type bug #83348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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. |
Update: 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
else:
return True |
It seems like this is a common problem : https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse . 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. |
Thank you for your quick and helpful reply. The problem with your solution is twofold:
For example: --flag1 a b I want to then run a command with the parameter combinations: 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. |
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 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. |
The rejected boolean type proposal: |
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. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: