Skip to content

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

Closed
NathanNaze mannequin opened this issue May 10, 2016 · 6 comments
Closed

unexpected behavior for booleans in argparse #71181

NathanNaze mannequin opened this issue May 10, 2016 · 6 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@NathanNaze
Copy link
Mannequin

NathanNaze mannequin commented May 10, 2016

BPO 26994
Nosy @bitdancer
Superseder
  • bpo-21208: Change default behavior of arguments with type bool when options are specified
  • 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:

    assignee = None
    closed_at = <Date 2016-05-10.18:37:48.266>
    created_at = <Date 2016-05-10.18:30:29.946>
    labels = ['type-bug', 'library']
    title = 'unexpected behavior for booleans in argparse'
    updated_at = <Date 2016-05-14.17:00:50.659>
    user = 'https://bugs.python.org/NathanNaze'

    bugs.python.org fields:

    activity = <Date 2016-05-14.17:00:50.659>
    actor = 'paul.j3'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-05-10.18:37:48.266>
    closer = 'r.david.murray'
    components = ['Library (Lib)']
    creation = <Date 2016-05-10.18:30:29.946>
    creator = 'Nathan Naze'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 26994
    keywords = []
    message_count = 6.0
    messages = ['265256', '265257', '265259', '265260', '265284', '265535']
    nosy_count = 3.0
    nosy_names = ['r.david.murray', 'paul.j3', 'Nathan Naze']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '21208'
    type = 'behavior'
    url = 'https://bugs.python.org/issue26994'
    versions = ['Python 2.7']

    @NathanNaze
    Copy link
    Mannequin Author

    NathanNaze mannequin commented May 10, 2016

    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.

    @NathanNaze NathanNaze mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 10, 2016
    @bitdancer
    Copy link
    Member

    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.

    @NathanNaze
    Copy link
    Mannequin Author

    NathanNaze mannequin commented May 10, 2016

    It does 'bool(value)', and 'bool("False")' is True, since "False" is a non-empty string.

    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.

    @NathanNaze
    Copy link
    Mannequin Author

    NathanNaze mannequin commented May 10, 2016

    I also buy the argument that changing the behavior now would be problematic given the existing usages in the wild.

    @bitdancer
    Copy link
    Member

    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.

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented May 14, 2016

    I answered a similar question recently on Stackoverflow when the user wanted to use type=hex.

    http://stackoverflow.com/questions/37006387/python-argparse-hex-error

    In another recent bug/issue the poster want a enum type. It's not hard to define a function, or factory class, that handles mappings like this, but it isn't as simple or intuitive as some would like.

    There is a registries mechanism, which could allow the user to use type='bool', where 'bool' is the name of a function that that converts some set of strings to True/False. But people are used to providing strings for the action, they aren't used to do so for the type.

    http://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant