[issue34803] argparse int type does not accept scientific notation

2018-09-27 Thread paul j3


Change by paul j3 :


--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34803] argparse int type does not accept scientific notation

2018-09-26 Thread paul j3


paul j3  added the comment:

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.

--
nosy: +paul.j3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34803] argparse int type does not accept scientific notation

2018-09-25 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34803] argparse int type does not accept scientific notation

2018-09-25 Thread Pablo Galindo Salgado


Pablo Galindo Salgado  added the comment:

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"] )

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34803] argparse int type does not accept scientific notation

2018-09-25 Thread Pablo Galindo Salgado


Pablo Galindo Salgado  added the comment:

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

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

--
nosy: +pablogsal

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34803] argparse int type does not accept scientific notation

2018-09-25 Thread Jesse Hostetler


Jesse Hostetler  added the comment:

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

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34803] argparse int type does not accept scientific notation

2018-09-25 Thread Jesse Hostetler


New submission from Jesse Hostetler :

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'

--
components: Library (Lib)
files: argparse-int-scientific.py
messages: 326409
nosy: jessehostetler
priority: normal
severity: normal
status: open
title: argparse int type does not accept scientific notation
type: enhancement
versions: Python 3.7
Added file: https://bugs.python.org/file47824/argparse-int-scientific.py

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com