-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
unexpected behavior for booleans in argparse #71181
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
Setting a boolean type in argparse gives unexpected behavior when setting "True", "False", etc. https://gist.github.com/nanaze/db63e3f63e318408e3223bf1245d9752 Would have expected parsing to fail for unclear input that doesn't neatly map to a boolean value. |
type=bool doesn't do what you think it does. It does 'bool(value)', and 'bool("False")' is True, since "False" is a non-empty string. See bpo-21208 for some further discussion. |
Yes, I understand this. It's fine to mark as "working as intended", but coming from other flag-parsing libraries, I find the behavior unintuitive and do not understand the utility of accepting arbitrary strings given the potential for user confusion. We uncovered this behavior debugging a script used internally at Google. |
I also buy the argument that changing the behavior now would be problematic given the existing usages in the wild. |
Yes, it is an unitended consequence of the fact that argparse types are arbitrary single argument functions (that take an arbitrary string as the argument and convert it), and bool is a single argument function. Unfortunately we're stuck with it now. The correct answer, of course, is to write your own bool type converter if you need one. It is possible there should be a documentation mention that bool is not approprate as a type function, since int and float, for example, are explicitly mentioned. If you want to submit a patch to that end I'll reopen the issue. |
I answered a similar question recently on Stackoverflow when the user wanted to use http://stackoverflow.com/questions/37006387/python-argparse-hex-error In another recent bug/issue the poster want a There is a registries mechanism, which could allow the user to use http://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse |
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: