diff options
| author | Bas van Beek <43369155+BvB93@users.noreply.github.com> | 2021-05-18 22:53:29 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-18 13:53:29 -0700 |
| commit | 39110f34ebc5e445ebea59833e621e4404c38be4 (patch) | |
| tree | 390cae493e01d462d6e863392a1da5a79f358c7a | |
| parent | df821062b1f2fa2b6d689cef5b89bf76ad9df4d0 (diff) | |
| download | numpy-39110f34ebc5e445ebea59833e621e4404c38be4.tar.gz | |
DEP: Deprecate 4 `ndarray.ctypes` methods (#19031)
* DEP: Deprecate 4 `ndarray.ctypes` methods
* `get_data`
* `get_shape`
* `get_strides`
* `get_as_parameter`
* TST: Add deprecation tests for 4 `ndarray.ctypes` methods
* DOC: Add a release note for the `ndarray.ctypes` method deprecation
* MAINT: Deprecate via `__getattr__` instead of `__getattribute__
Co-Authored-By: Sebastian Berg <sebastian@sipsolutions.net>
* MAINT: Deprecate the methods via proper function definitions
Co-Authored-By: Eric Wieser <425260+eric-wieser@users.noreply.github.com>
* DOC: Added a missing "the"
* DOC: typo fix: property -> properties
Co-Authored-By: Eric Wieser <425260+eric-wieser@users.noreply.github.com>
Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
Co-authored-by: Eric Wieser <425260+eric-wieser@users.noreply.github.com>
| -rw-r--r-- | doc/release/upcoming_changes/19031.deprecation.rst | 12 | ||||
| -rw-r--r-- | doc/source/user/misc.rst | 11 | ||||
| -rw-r--r-- | numpy/core/_internal.py | 45 | ||||
| -rw-r--r-- | numpy/core/tests/test_deprecations.py | 21 |
4 files changed, 78 insertions, 11 deletions
diff --git a/doc/release/upcoming_changes/19031.deprecation.rst b/doc/release/upcoming_changes/19031.deprecation.rst new file mode 100644 index 000000000..de92e18df --- /dev/null +++ b/doc/release/upcoming_changes/19031.deprecation.rst @@ -0,0 +1,12 @@ +Four `ndarray.ctypes` methods have been deprecated +-------------------------------------------------- +Four methods of the `ndarray.ctypes` object have been deprecated, +as they are (undocumentated) implementation artifacts of their respective +properties. + +The methods in question are: + +* ``_ctypes.get_data`` (use ``_ctypes.data`` instead) +* ``_ctypes.get_shape`` (use ``_ctypes.shape`` instead) +* ``_ctypes.get_strides`` (use ``_ctypes.strides`` instead) +* ``_ctypes.get_as_parameter`` (use ``_ctypes._as_parameter_`` instead) diff --git a/doc/source/user/misc.rst b/doc/source/user/misc.rst index 031ce4efa..f0a7f5e4c 100644 --- a/doc/source/user/misc.rst +++ b/doc/source/user/misc.rst @@ -149,11 +149,12 @@ Only a survey of the choices. Little detail on how each works. - good numpy support: arrays have all these in their ctypes attribute: :: - a.ctypes.data a.ctypes.get_strides - a.ctypes.data_as a.ctypes.shape - a.ctypes.get_as_parameter a.ctypes.shape_as - a.ctypes.get_data a.ctypes.strides - a.ctypes.get_shape a.ctypes.strides_as + a.ctypes.data + a.ctypes.data_as + a.ctypes.shape + a.ctypes.shape_as + a.ctypes.strides + a.ctypes.strides_as - Minuses: diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index 449926f58..4eebbaea3 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -8,6 +8,7 @@ import ast import re import sys import platform +import warnings from .multiarray import dtype, array, ndarray try: @@ -350,11 +351,45 @@ class _ctypes: """ return self.data_as(ctypes.c_void_p) - # kept for compatibility - get_data = data.fget - get_shape = shape.fget - get_strides = strides.fget - get_as_parameter = _as_parameter_.fget + # Numpy 1.21.0, 2021-05-18 + + def get_data(self): + """Deprecated getter for the `_ctypes.data` property. + + .. deprecated:: 1.21 + """ + warnings.warn('"get_data" is deprecated. Use "data" instead', + DeprecationWarning, stacklevel=2) + return self.data + + def get_shape(self): + """Deprecated getter for the `_ctypes.shape` property. + + .. deprecated:: 1.21 + """ + warnings.warn('"get_shape" is deprecated. Use "shape" instead', + DeprecationWarning, stacklevel=2) + return self.shape + + def get_strides(self): + """Deprecated getter for the `_ctypes.strides` property. + + .. deprecated:: 1.21 + """ + warnings.warn('"get_strides" is deprecated. Use "strides" instead', + DeprecationWarning, stacklevel=2) + return self.strides + + def get_as_parameter(self): + """Deprecated getter for the `_ctypes._as_parameter_` property. + + .. deprecated:: 1.21 + """ + warnings.warn( + '"get_as_parameter" is deprecated. Use "_as_parameter_" instead', + DeprecationWarning, stacklevel=2, + ) + return self._as_parameter_ def _newnames(datatype, order): diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index ed1688374..6eab2505d 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -906,7 +906,7 @@ else: class TestNoseDecoratorsDeprecated(_DeprecationTestCase): class DidntSkipException(Exception): pass - + def test_slow(self): def _test_slow(): @np.testing.dec.slow @@ -1172,3 +1172,22 @@ class TestComparisonBadObjectDType(_DeprecationTestCase): self.assert_deprecated(lambda: np.equal(1, 1, dtype=object)) self.assert_deprecated( lambda: np.equal(1, 1, sig=(None, None, object))) + + +class TestCtypesGetter(_DeprecationTestCase): + # Deprecated 2021-05-18, Numpy 1.21.0 + warning_cls = DeprecationWarning + ctypes = np.array([1]).ctypes + + @pytest.mark.parametrize( + "name", ["get_data", "get_shape", "get_strides", "get_as_parameter"] + ) + def test_deprecated(self, name: str) -> None: + func = getattr(self.ctypes, name) + self.assert_deprecated(lambda: func()) + + @pytest.mark.parametrize( + "name", ["data", "shape", "strides", "_as_parameter_"] + ) + def test_not_deprecated(self, name: str) -> None: + self.assert_not_deprecated(lambda: getattr(self.ctypes, name)) |
