Skip to content
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

argparse int type does not accept scientific notation #78984

Closed
jessehostetler mannequin opened this issue Sep 25, 2018 · 5 comments
Closed

argparse int type does not accept scientific notation #78984

jessehostetler mannequin opened this issue Sep 25, 2018 · 5 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@jessehostetler
Copy link
Mannequin

jessehostetler mannequin commented Sep 25, 2018

BPO 34803
Nosy @pablogsal, @tirkarthi
Files
  • argparse-int-scientific.py: Code example
  • 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 2018-09-27.16:56:48.645>
    created_at = <Date 2018-09-25.22:31:02.420>
    labels = ['3.7', 'invalid', 'type-feature', 'library']
    title = 'argparse int type does not accept scientific notation'
    updated_at = <Date 2018-09-27.16:56:48.645>
    user = 'https://bugs.python.org/jessehostetler'

    bugs.python.org fields:

    activity = <Date 2018-09-27.16:56:48.645>
    actor = 'paul.j3'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-09-27.16:56:48.645>
    closer = 'paul.j3'
    components = ['Library (Lib)']
    creation = <Date 2018-09-25.22:31:02.420>
    creator = 'jessehostetler'
    dependencies = []
    files = ['47824']
    hgrepos = []
    issue_num = 34803
    keywords = []
    message_count = 5.0
    messages = ['326409', '326410', '326413', '326414', '326520']
    nosy_count = 4.0
    nosy_names = ['paul.j3', 'pablogsal', 'xtreak', 'jessehostetler']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue34803'
    versions = ['Python 3.7']

    @jessehostetler
    Copy link
    Mannequin Author

    jessehostetler mannequin commented Sep 25, 2018

    The 'argparse' module gives a parse error for integer arguments written in scientific notation. I would expect this to work, since integers can be constructed from literals in scientific notation.

    Example (also attached):

    import argparse
    foo = int(1e3) # Works: foo = 1000
    parser = argparse.ArgumentParser()
    parser.add_argument( "--foo", type=int )
    parser.parse_args( ["--foo=1e3"] )
    # error: argument --foo: invalid int value: '1e3'

    @jessehostetler jessehostetler mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Sep 25, 2018
    @jessehostetler
    Copy link
    Mannequin Author

    jessehostetler mannequin commented Sep 25, 2018

    I suppose desired behavior would be to accept the argument only if it can be converted to int without loss of information.

    @pablogsal
    Copy link
    Member

    The conversion fails because is trying to convert a string, not a float:

    >>> int("1e3")
    *** ValueError: invalid literal for int() with base 10: '1e3'

    @pablogsal
    Copy link
    Member

    You can always do:

    import argparse
    foo = int(1e3) # Works: foo = 1000
    parser = argparse.ArgumentParser()
    parser.add_argument( "--foo", type=lambda x: int(float(x)))
    parser.parse_args( ["--foo=1e3"] )

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Sep 27, 2018

    The type parameter is normally a function (or more generally a callable). When given a string it should convert it as needed, or raise an error. In your example that function is the stock, 'int()'.

    Test int('123'), int('1e3') etc for yourself to see what it can handle.

    If you want to convert '1e3' to an integer, write your own function that handle it. Don't expect argparse or the stock int() to do it for you.

    More commonly people use 'type=bool', expecting it convert 'True' or 'False' strings to boolean values. But that's not what the bool() function does.

    To reiterate, 'type' is a function, not a desired class.

    Since this is not a bug, I think this should be closed.

    @paulj3 paulj3 mannequin closed this as completed Sep 27, 2018
    @paulj3 paulj3 mannequin added the invalid label Sep 27, 2018
    @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
    3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant