python 2.7 range accepts numpy float16, float32, complex when it should raise TypeError
See original GitHub issueIn python 2.7, range accepts numpy.float16
and np.float32
, np.float128
, complex64
and complex128
as inputs, which it really shouldn’t.
Reproducing code example:
>>> import numpy as np
>>> range(np.float16(4))
[0, 1, 2, 3]
>>> range(np.float32(4))
[0, 1, 2, 3]
>>> range(np.complex64(4.))
/home/mark/miniconda3/envs/np2/bin/ipython:1: ComplexWarning: Casting complex values to real discards the imaginary part
#!/home/mark/miniconda3/envs/np2/bin/python
[0, 1, 2, 3]
>>> range(np.complex128(4.))
/home/mark/miniconda3/envs/np2/bin/ipython:1: ComplexWarning: Casting complex values to real discards the imaginary part
#!/home/mark/miniconda3/envs/np2/bin/python
[0, 1, 2, 3]
>>> range(np.float128(1))
[0]
Success??? But these fail (as expected)
>>> import numpy as np
>>> range(np.float(4))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: range() integer end argument expected, got float.
>>> import numpy as np
>>> range(np.float64(4))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: range() integer end argument expected, got numpy.float64.
Not sure if this is a python bug, or numpy bug.
In python3:
>>> import numpy as np
>>> range(np.float16(4))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'numpy.float16' object cannot be interpreted as an integer
>>> range(np.float32(4))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'numpy.float32' object cannot be interpreted as an integer
>>> range(np.complex64(4.))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'numpy.complex64' object cannot be interpreted as an integer
>>> range(np.complex128(4.))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'numpy.complex128' object cannot be interpreted as an integer
>>> range(np.float(4))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
>>> range(np.float64(4))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'numpy.float64' object cannot be interpreted as an integer
>>> range(np.float128(1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'numpy.float128' object cannot be interpreted as an integer
Numpy/Python version information:
Python 2.7
>>> import sys, numpy; print(numpy.__version__, sys.version)
('1.16.0.dev0+b99c73d', '2.7.15 |Anaconda, Inc.| (default, Sep 27 2018, 20:19:23) \n[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]')
or
>>> import sys, numpy; print(numpy.__version__, sys.version)
('1.15.2', '2.7.15 |Anaconda, Inc.| (default, Sep 27 2018, 20:19:23) \n[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]')
Python 3.6
>>> import sys, numpy; print(numpy.__version__, sys.version)
1.15.2 3.6.6 | packaged by conda-forge | (default, Jul 26 2018, 09:53:17)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Converting numpy dtypes to native python types
Use val.item() to convert most NumPy values to a native Python type: import numpy as np # for example, numpy.float32 -> python float...
Read more >NEP 50 — Promotion rules for Python scalars - NumPy
For a Python int , float , or complex the value is inspected as ... np.arange(10, dtype=np.int8) + 256 # raises a TypeError...
Read more >Chapter 4. NumPy Basics: Arrays and Vectorized Computation
arange is an array-valued version of the built-in Python range function: ... a string that cannot be converted to float64 ), a TypeError...
Read more >Python Range of Float Numbers - PYnative
Use Python's numpy arange() and linspace() functions to generate a range of float numbers. Use decimal numbers as start, stop, ...
Read more >Change data type of given numpy array - GeeksforGeeks
In order to change the dtype of the given array object, we will use numpy.astype() function. The function takes an argument which is...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Well, python fixed it already, its part of python 3 😃.
It isn’t really likely that anybody outside the scientific community will want to fix this though.