Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 91b06c0

Browse filesBrowse files
authored
Merge pull request #9522 from eric-wieser/stop-using-obj2sctype
BUG: Fix problems with obj2sctype
2 parents f307cec + 351fe97 commit 91b06c0
Copy full SHA for 91b06c0

File tree

Expand file treeCollapse file tree

5 files changed

+36
-46
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+36
-46
lines changed

‎doc/release/1.14.0-notes.rst

Copy file name to clipboardExpand all lines: doc/release/1.14.0-notes.rst
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ This is not expected to cause problems, but possibly something has been left
6868
out. If you experience an unexpected import problem using ``numpy.testing``
6969
let us know.
7070

71+
``np.asfarray`` no longer accepts non-dtypes through the ``dtype`` argument
72+
---------------------------------------------------------------------------
73+
This previously would accept ``dtype=some_array``, with the implied semantics
74+
of ``dtype=some_array.dtype``. This was undocumented, unique across the numpy
75+
functions, and if used would likely correspond to a typo.
76+
77+
1D ``np.linalg.norm`` preserves float input types, even for arbitrary orders
78+
----------------------------------------------------------------------------
79+
Previously, this would promote to ``float64`` when arbitrary orders were
80+
passed, despite not doing so under the simple cases::
81+
82+
>>> f32 = np.float32([1, 2])
83+
>>> np.linalg.norm(f32, 2.0).dtype
84+
dtype('float32')
85+
>>> np.linalg.norm(f32, 2.0001).dtype
86+
dtype('float64') # numpy 1.13
87+
dtype('float32') # numpy 1.14
88+
89+
This change affects only ``float32`` and ``float16`` arrays.
90+
7191

7292
C API changes
7393
=============

‎numpy/core/numerictypes.py

Copy file name to clipboardExpand all lines: numpy/core/numerictypes.py
+7-31Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -529,27 +529,6 @@ def maximum_sctype(t):
529529
else:
530530
return sctypes[base][-1]
531531

532-
try:
533-
buffer_type = _types.BufferType
534-
except AttributeError:
535-
# Py3K
536-
buffer_type = memoryview
537-
538-
_python_types = {int: 'int_',
539-
float: 'float_',
540-
complex: 'complex_',
541-
bool: 'bool_',
542-
bytes: 'bytes_',
543-
unicode: 'unicode_',
544-
buffer_type: 'void',
545-
}
546-
547-
def _python_type(t):
548-
""" Get a numpy scalar type corresponding to a Python type or value """
549-
if not isinstance(t, type):
550-
t = type(t)
551-
return allTypes[_python_types.get(t, 'object_')]
552-
553532

554533
def issctype(rep):
555534
"""
@@ -634,22 +613,19 @@ def obj2sctype(rep, default=None):
634613
<type 'list'>
635614
636615
"""
637-
try:
638-
if issubclass(rep, generic):
639-
return rep
640-
except TypeError:
641-
pass
642-
if isinstance(rep, dtype):
643-
return rep.type
644-
if isinstance(rep, type):
645-
return _python_type(rep)
616+
# prevent abtract classes being upcast
617+
if isinstance(rep, type) and issubclass(rep, generic):
618+
return rep
619+
# extract dtype from arrays
646620
if isinstance(rep, ndarray):
647621
return rep.dtype.type
622+
# fall back on dtype to convert
648623
try:
649624
res = dtype(rep)
650625
except Exception:
651626
return default
652-
return res.type
627+
else:
628+
return res.type
653629

654630

655631
def issubclass_(arg1, arg2):

‎numpy/lib/tests/test_type_check.py

Copy file name to clipboardExpand all lines: numpy/lib/tests/test_type_check.py
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
from numpy.compat import long
55
from numpy.testing import (
6-
assert_, assert_equal, assert_array_equal, run_module_suite
6+
assert_, assert_equal, assert_array_equal, run_module_suite, assert_raises
77
)
88
from numpy.lib.type_check import (
99
common_type, mintypecode, isreal, iscomplex, isposinf, isneginf,
@@ -422,5 +422,11 @@ def test_asfarray(self):
422422
assert_equal(a.__class__, np.ndarray)
423423
assert_(np.issubdtype(a.dtype, np.floating))
424424

425+
# previously this would infer dtypes from arrays, unlike every single
426+
# other numpy function
427+
assert_raises(TypeError,
428+
asfarray, np.array([1, 2, 3]), dtype=np.array(1.0))
429+
430+
425431
if __name__ == "__main__":
426432
run_module_suite()

‎numpy/lib/type_check.py

Copy file name to clipboardExpand all lines: numpy/lib/type_check.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ def asfarray(a, dtype=_nx.float_):
9898
array([ 2., 3.])
9999
100100
"""
101-
dtype = _nx.obj2sctype(dtype)
102-
if not issubclass(dtype, _nx.inexact):
101+
if not _nx.issubdtype(dtype, _nx.inexact):
103102
dtype = _nx.float_
104103
return asarray(a, dtype=dtype)
105104

‎numpy/linalg/linalg.py

Copy file name to clipboardExpand all lines: numpy/linalg/linalg.py
+1-12Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,18 +2201,7 @@ def norm(x, ord=None, axis=None, keepdims=False):
22012201
ord + 1
22022202
except TypeError:
22032203
raise ValueError("Invalid norm order for vectors.")
2204-
if x.dtype.type is longdouble:
2205-
# Convert to a float type, so integer arrays give
2206-
# float results. Don't apply asfarray to longdouble arrays,
2207-
# because it will downcast to float64.
2208-
absx = abs(x)
2209-
else:
2210-
absx = x if isComplexType(x.dtype.type) else asfarray(x)
2211-
if absx.dtype is x.dtype:
2212-
absx = abs(absx)
2213-
else:
2214-
# if the type changed, we can safely overwrite absx
2215-
abs(absx, out=absx)
2204+
absx = abs(x)
22162205
absx **= ord
22172206
return add.reduce(absx, axis=axis, keepdims=keepdims) ** (1.0 / ord)
22182207
elif len(axis) == 2:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.