diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2017-08-06 17:22:20 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-06 17:22:20 -0500 |
commit | 91b06c021319faccb008a8ee485d26ae227bf079 (patch) | |
tree | 7195085d6bcaec87b5f2f0629c14456fcfa53d11 /numpy | |
parent | f307cec3962926558b6387ebb4ab8d3f4ea3aa34 (diff) | |
parent | 351fe979580ab842cc5c04e1b616d3a64936a695 (diff) | |
download | numpy-91b06c021319faccb008a8ee485d26ae227bf079.tar.gz |
Merge pull request #9522 from eric-wieser/stop-using-obj2sctype
BUG: Fix problems with obj2sctype
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/numerictypes.py | 38 | ||||
-rw-r--r-- | numpy/lib/tests/test_type_check.py | 8 | ||||
-rw-r--r-- | numpy/lib/type_check.py | 3 | ||||
-rw-r--r-- | numpy/linalg/linalg.py | 13 |
4 files changed, 16 insertions, 46 deletions
diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index 2cb66aab4..b61f5e7bc 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -529,27 +529,6 @@ def maximum_sctype(t): else: return sctypes[base][-1] -try: - buffer_type = _types.BufferType -except AttributeError: - # Py3K - buffer_type = memoryview - -_python_types = {int: 'int_', - float: 'float_', - complex: 'complex_', - bool: 'bool_', - bytes: 'bytes_', - unicode: 'unicode_', - buffer_type: 'void', - } - -def _python_type(t): - """ Get a numpy scalar type corresponding to a Python type or value """ - if not isinstance(t, type): - t = type(t) - return allTypes[_python_types.get(t, 'object_')] - def issctype(rep): """ @@ -634,22 +613,19 @@ def obj2sctype(rep, default=None): <type 'list'> """ - try: - if issubclass(rep, generic): - return rep - except TypeError: - pass - if isinstance(rep, dtype): - return rep.type - if isinstance(rep, type): - return _python_type(rep) + # prevent abtract classes being upcast + if isinstance(rep, type) and issubclass(rep, generic): + return rep + # extract dtype from arrays if isinstance(rep, ndarray): return rep.dtype.type + # fall back on dtype to convert try: res = dtype(rep) except Exception: return default - return res.type + else: + return res.type def issubclass_(arg1, arg2): diff --git a/numpy/lib/tests/test_type_check.py b/numpy/lib/tests/test_type_check.py index e05079981..8945b61ea 100644 --- a/numpy/lib/tests/test_type_check.py +++ b/numpy/lib/tests/test_type_check.py @@ -3,7 +3,7 @@ from __future__ import division, absolute_import, print_function import numpy as np from numpy.compat import long from numpy.testing import ( - assert_, assert_equal, assert_array_equal, run_module_suite + assert_, assert_equal, assert_array_equal, run_module_suite, assert_raises ) from numpy.lib.type_check import ( common_type, mintypecode, isreal, iscomplex, isposinf, isneginf, @@ -422,5 +422,11 @@ class TestArrayConversion(object): assert_equal(a.__class__, np.ndarray) assert_(np.issubdtype(a.dtype, np.floating)) + # previously this would infer dtypes from arrays, unlike every single + # other numpy function + assert_raises(TypeError, + asfarray, np.array([1, 2, 3]), dtype=np.array(1.0)) + + if __name__ == "__main__": run_module_suite() diff --git a/numpy/lib/type_check.py b/numpy/lib/type_check.py index b2de153d3..e6aae8ddd 100644 --- a/numpy/lib/type_check.py +++ b/numpy/lib/type_check.py @@ -98,8 +98,7 @@ def asfarray(a, dtype=_nx.float_): array([ 2., 3.]) """ - dtype = _nx.obj2sctype(dtype) - if not issubclass(dtype, _nx.inexact): + if not _nx.issubdtype(dtype, _nx.inexact): dtype = _nx.float_ return asarray(a, dtype=dtype) diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py index 0f1966a9b..cd8999785 100644 --- a/numpy/linalg/linalg.py +++ b/numpy/linalg/linalg.py @@ -2201,18 +2201,7 @@ def norm(x, ord=None, axis=None, keepdims=False): ord + 1 except TypeError: raise ValueError("Invalid norm order for vectors.") - if x.dtype.type is longdouble: - # Convert to a float type, so integer arrays give - # float results. Don't apply asfarray to longdouble arrays, - # because it will downcast to float64. - absx = abs(x) - else: - absx = x if isComplexType(x.dtype.type) else asfarray(x) - if absx.dtype is x.dtype: - absx = abs(absx) - else: - # if the type changed, we can safely overwrite absx - abs(absx, out=absx) + absx = abs(x) absx **= ord return add.reduce(absx, axis=axis, keepdims=keepdims) ** (1.0 / ord) elif len(axis) == 2: |