diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2020-02-07 11:33:45 -0800 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2020-02-07 13:47:19 -0800 |
commit | cefd3523a63cfd5ede693d53d3821e37fa8ca158 (patch) | |
tree | 627bd350cef3c2dbf81b6efa51b58c5d108475ed | |
parent | 1a1611a33cfb5ea50d16d20affa5c6fa03e148d7 (diff) | |
download | numpy-cefd3523a63cfd5ede693d53d3821e37fa8ca158.tar.gz |
Fixup: Do not deprecate generic python types
Add a comment that we can think about only allowing it for `dtype=...`
though...
-rw-r--r-- | doc/source/reference/arrays.dtypes.rst | 16 | ||||
-rw-r--r-- | numpy/core/src/multiarray/descriptor.c | 14 | ||||
-rw-r--r-- | numpy/core/tests/test_deprecations.py | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 2 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 2 | ||||
-rw-r--r-- | numpy/core/tests/test_numerictypes.py | 10 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 2 |
7 files changed, 32 insertions, 23 deletions
diff --git a/doc/source/reference/arrays.dtypes.rst b/doc/source/reference/arrays.dtypes.rst index 231707b11..71c2fa5a4 100644 --- a/doc/source/reference/arrays.dtypes.rst +++ b/doc/source/reference/arrays.dtypes.rst @@ -156,6 +156,14 @@ Array-scalar types Generic types + .. deprecated NumPy 1.19:: + + The use of generic types is deprecated. This is because it can be + unexpected in a context such as ``arr.astype(dtype=np.floating)``. + ``arr.astype(dtype=np.floating)`` which casts an array of ``float32`` + to an array of ``float64``, even though ``float32`` is a subdtype of + ``np.floating``. + The generic hierarchical type objects convert to corresponding type objects according to the associations: @@ -179,8 +187,7 @@ Built-in Python types :class:`float` :class:`float\_` :class:`complex` :class:`cfloat` :class:`bytes` :class:`bytes\_` - :class:`str` :class:`bytes\_` (Python2) or :class:`unicode\_` (Python3) - :class:`unicode` :class:`unicode\_` + :class:`str` :class:`str\_` :class:`buffer` :class:`void` (all others) :class:`object_` ================ =============== @@ -196,6 +203,11 @@ Built-in Python types >>> dt = np.dtype(int) # Python-compatible integer >>> dt = np.dtype(object) # Python object + .. note:: + + All other types map to ``object_`` for convenience. Code should expect + that such types may map to a specific (new) dtype in future the future. + Types with ``.dtype`` Any type object with a ``dtype`` attribute: The attribute will be diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index 0079aa86e..ffbb02ce8 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -1436,13 +1436,13 @@ _convert_from_type(PyObject *obj) { } Py_DECREF(ret); - if (DEPRECATE("Converting a type/class not known to NumPy to a dtype " - "currently always returns `np.dtype(object)`. This loses " - "the type information and is deprecated.") < 0) { - return NULL; - } - - /* All other classes are treated as object */ + /* + * All other classes are treated as object. This can be convenient + * to convey an intention of using it for a specific python type + * and possibly allow future enforcement. It may make sense to + * only allow this only within `dtype=...` keyword argument context + * in the future. + */ return PyArray_DescrFromType(NPY_OBJECT); } } diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index a89fc70d5..d2cf315a9 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -548,7 +548,7 @@ def test_deprecate_ragged_arrays(): np.array(arg) -class TestAbstractDTypeCoercion(_DeprecationTestCase): +class TestDTypeCoercion(_DeprecationTestCase): # 2020-02-06 1.19.0 message = "Converting .* to a dtype .*is deprecated" deprecated_types = [ @@ -558,9 +558,6 @@ class TestAbstractDTypeCoercion(_DeprecationTestCase): np.integer, np.unsignedinteger, np.signedinteger, # character is a deprecated S1 special case: np.character, - # Test python types that do not map to a NumPy type cleanly - # (currenlty map to object) - type, list, tuple, dict, ] def test_dtype_coercion(self): @@ -576,3 +573,7 @@ class TestAbstractDTypeCoercion(_DeprecationTestCase): for group in np.sctypes.values(): for scalar_type in group: self.assert_not_deprecated(np.dtype, args=(scalar_type,)) + + for scalar_type in [type, dict, list, tuple]: + # Typical python types are coerced to object currently: + self.assert_not_deprecated(np.dtype, args=(scalar_type,)) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index f6181c900..ad38911cb 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -6456,7 +6456,7 @@ class TestRepeat: NEIGH_MODE = {'zero': 0, 'one': 1, 'constant': 2, 'circular': 3, 'mirror': 4} -@pytest.mark.parametrize('dt', [float, object], ids=['float', 'object']) +@pytest.mark.parametrize('dt', [float, Decimal], ids=['float', 'object']) class TestNeighborhoodIter: # Simple, 2d tests def test_simple2d(self, dt): diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 0f7ac036f..3bc4cd187 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -2543,7 +2543,7 @@ class TestCorrelate: assert_array_almost_equal(z, self.zs) def test_object(self): - self._setup(object) + self._setup(Decimal) z = np.correlate(self.x, self.y, 'full') assert_array_almost_equal(z, self.z1) z = np.correlate(self.y, self.x, 'full') diff --git a/numpy/core/tests/test_numerictypes.py b/numpy/core/tests/test_numerictypes.py index 22fc2ed58..c72d13947 100644 --- a/numpy/core/tests/test_numerictypes.py +++ b/numpy/core/tests/test_numerictypes.py @@ -3,8 +3,7 @@ import itertools import pytest import numpy as np -from numpy.testing import ( - assert_, assert_equal, assert_raises, assert_warns, IS_PYPY) +from numpy.testing import assert_, assert_equal, assert_raises, IS_PYPY # This is the structure of the table used for plain objects: # @@ -452,11 +451,8 @@ class Test_sctype2char: def test_other_type(self): assert_equal(np.sctype2char(float), 'd') - assert_equal(np.sctype2char(object), 'O') - with assert_warns(DeprecationWarning): - assert_equal(np.sctype2char(list), 'O') - with assert_warns(DeprecationWarning): - assert_equal(np.sctype2char(np.ndarray), 'O') + assert_equal(np.sctype2char(list), 'O') + assert_equal(np.sctype2char(np.ndarray), 'O') def test_third_party_scalar_type(self): from numpy.core._rational_tests import rational diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 50a543625..321723b9b 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -1152,7 +1152,7 @@ class TestRegression: assert_(dat.argmax(1).info == 'jubba') assert_(dat.argmin(1).info == 'jubba') assert_(dat.argsort(1).info == 'jubba') - assert_(dat.astype(object).info == 'jubba') + assert_(dat.astype(TestArray).info == 'jubba') assert_(dat.byteswap().info == 'jubba') assert_(dat.clip(2, 7).info == 'jubba') assert_(dat.compress([0, 1, 1]).info == 'jubba') |