summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorMarten van Kerkwijk <mhvk@astro.utoronto.ca>2022-01-08 17:38:46 -0500
committerMarten van Kerkwijk <mhvk@astro.utoronto.ca>2022-01-09 17:35:05 -0500
commit854dafad6a27f3a9bf915b21e7b28d44343412c9 (patch)
tree09e0c2f66aba3adc1c4c952a6b8fba7ecb54546a /numpy
parent176f66a2560e2e053ccb2c6b344246816f305526 (diff)
downloadnumpy-854dafad6a27f3a9bf915b21e7b28d44343412c9.tar.gz
DEP: warn that __array_finalize__ = None is deprecated
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/ctors.c17
-rw-r--r--numpy/core/tests/test_deprecations.py11
-rw-r--r--numpy/core/tests/test_multiarray.py8
3 files changed, 27 insertions, 9 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index 2ce648998..25eb91977 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -891,9 +891,24 @@ PyArray_NewFromDescr_int(
if (func == NULL) {
goto fail;
}
- else if (func == ndarray_array_finalize || func == Py_None) {
+ else if (func == ndarray_array_finalize) {
Py_DECREF(func);
}
+ else if (func == Py_None) {
+ Py_DECREF(func);
+ /*
+ * 2022-01-08, NumPy 1.23; when deprecation period is over, remove this
+ * whole stanza so one gets a "NoneType object is not callable" TypeError.
+ */
+ if (DEPRECATE(
+ "Setting __array_finalize__ = None to indicate no finalization"
+ "should be done is deprecated. Instead, just inherit from "
+ "ndarray or, if that is not possible, explicitly set to "
+ "ndarray.__array_function__; this will raise a TypeError "
+ "in the future. (Deprecated since NumPy 1.23)") < 0) {
+ goto fail;
+ }
+ }
else {
if (PyCapsule_CheckExact(func)) {
/* A C-function is stored here */
diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py
index 76486f755..d2a69d4cf 100644
--- a/numpy/core/tests/test_deprecations.py
+++ b/numpy/core/tests/test_deprecations.py
@@ -1239,3 +1239,14 @@ class TestMemEventHook(_DeprecationTestCase):
with pytest.warns(DeprecationWarning,
match='PyDataMem_SetEventHook is deprecated'):
ma_tests.test_pydatamem_seteventhook_end()
+
+
+class TestArrayFinalizeNone(_DeprecationTestCase):
+ message = "Setting __array_finalize__ = None"
+
+ def test_use_none_is_deprecated(self):
+ # Deprecated way that ndarray itself showed nothing needs finalizing.
+ class NoFinalize(np.ndarray):
+ __array_finalize__ = None
+
+ self.assert_deprecated(lambda: np.array(1).view(NoFinalize))
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index 8f4851036..708e82910 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -9021,14 +9021,6 @@ class TestArrayFinalize:
a = np.array(1).view(SuperFinalize)
assert_(a.saved_result is None)
- def test_can_use_none(self):
- # For backward compatibility, to show nothing needs finalizing.
- class NoFinalize(np.ndarray):
- __array_finalize__ = None
-
- a = np.array(1).view(NoFinalize)
- assert isinstance(a, NoFinalize)
-
def test_orderconverter_with_nonASCII_unicode_ordering():
# gh-7475