8000 MAINT: revert __skip_array_function__ from NEP-18 · shoyer/numpy@766281d · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit 766281d

Browse files
committed
MAINT: revert __skip_array_function__ from NEP-18
xref numpyGH-13624, numpyGH-12028 TODO: update tests/CI for NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=0
1 parent cf704e7 commit 766281d

File tree

5 files changed

+33
-29
lines changed

5 files changed

+33
-29
lines changed

numpy/core/overrides.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
"""Implementation of __array_function__ overrides from NEP-18."""
22
import collections
33
import functools
4+
import os
45
import textwrap
56

67
from numpy.core._multiarray_umath import (
78
add_docstring, implement_array_function, _get_implementing_args)
89
from numpy.compat._inspect import getargspec
910

1011

12+
ENABLE_ARRAY_FUNCTION = bool(
13+
int(os.environ.get('NUMPY_EXPERIMENTAL_ARRAY_FUNCTION', 1)))
14+
15+
1116
add_docstring(
1217
implement_array_function,
1318
"""
@@ -137,6 +142,22 @@ def array_function_dispatch(dispatcher, module=None, verify=True,
137142
Function suitable for decorating the implementation of a NumPy function.
138143
"""
139144

145+
if not ENABLE_ARRAY_FUNCTION:
146+
def decorator(implementation):
147+
if docs_from_dispatcher:
148+
add_docstring(implementation, dispatcher.__doc__)
149+
150+
public_api = implementation
151+
152+
if module is not None:
153+
public_api.__module__ = module
154+
155+
public_api._implementation = implementation
156+
157+
return public_api
158+
159+
return decorator
160+
140161
def decorator(implementation):
141162
if verify:
142163
verify_matching_signatures(implementation, dispatcher)
@@ -172,7 +193,7 @@ def {name}(*args, **kwargs):
172193
if module is not None:
173194
public_api.__module__ = module
174195

175-
public_api.__skip_array_function__ = 8000 implementation
196+
public_api._implementation = implementation
176197

177198
return public_api
178199

numpy/core/src/multiarray/arrayfunction_override.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ array_function_method_impl(PyObject *func, PyObject *types, PyObject *args,
173173
}
174174
}
175175

176-
implementation = PyObject_GetAttr(func, npy_ma_str_skip_array_function);
176+
implementation = PyObject_GetAttr(func, npy_ma_str_implementation);
177177
if (implementation == NULL) {
178178
return NULL;
179179
}

numpy/core/src/multiarray/multiarraymodule.c

Lines changed: 3 additions & 4 deletions
Original file line number
Diff line numberDiff line change
@@ -4498,7 +4498,7 @@ NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_prepare = NULL;
44984498
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_wrap = NULL;
44994499
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_array_finalize = NULL;
45004500
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_ufunc = NULL;
4501-
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_skip_array_function = NULL;
4501+
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_implementation = NULL;
45024502
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_order = NULL;
45034503
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_copy = NULL;
45044504
NPY_VISIBILITY_HIDDEN PyObject * npy_ma_str_dtype = NULL;
@@ -4514,8 +4514,7 @@ intern_strings(void)
45144514
npy_ma_str_array_wrap = PyUString_InternFromString("__array_wrap__");
45154515
npy_ma_str_array_finalize = PyUString_InternFromString("__array_finalize__");
45164516
npy_ma_str_ufunc = PyUString_InternFromString("__array_ufunc__");
4517-
npy_ma_str_skip_array_function = PyUString_InternFromString(
4518-
"__skip_array_function__");
4517+
npy_ma_str_implementation = PyUString_InternFromString("_implementation");
45194518
npy_ma_str_order = PyUString_InternFromString("order");
45204519
npy_ma_str_copy = PyUString_InternFromString("copy");
45214520
npy_ma_str_dtype = PyUString_InternFromString("dtype");
@@ -4525,7 +4524,7 @@ intern_strings(void)
45254524

45264525
return npy_ma_str_array && npy_ma_str_array_prepare &&
45274526
npy_ma_str_array_wrap && npy_ma_str_array_finalize &&
4528-
npy_ma_str_ufunc && npy_ma_str_skip_array_function &&
4527+
npy_ma_str_ufunc && npy_ma_str_implementation &&
45294528
npy_ma_str_order && npy_ma_str_copy && npy_ma_str_dtype &&
45304529
npy_ma_str_ndmin && npy_ma_str_axis1 && npy_ma_str_axis2;
45314530
}

numpy/core/src/multiarray/multiarraymodule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_prepare;
66
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_wrap;
77
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_array_finalize;
88
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_ufunc;
9-
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_skip_array_function;
9+
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_implementation;
1010
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_order;
1111
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_copy;
1212
NPY_VISIBILITY_HIDDEN extern PyObject * npy_ma_str_dtype;

numpy/core/tests/test_overrides.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,13 @@ class OverrideSub(np.ndarray):
190190
result = np.concatenate((array, override_sub))
191191
assert_equal(result, expected.view(OverrideSub))
192192

193-
def test_skip_array_function(self):
194-
assert_(dispatched_one_arg.__skip_array_function__
195-
is dispatched_one_arg.__wrapped__)
196-
197193
def test_no_wrapper(self):
198194
# This shouldn't happen unless a user intentionally calls
199195
# __array_function__ with invalid arguments, but check that we raise
200196
# an appropriate error all the same.
201197
array = np.array(1)
202-
func = dispatched_one_arg.__skip_array_function__
203-
with assert_raises_regex(AttributeError, '__skip_array_function__'):
198+
func = dispatched_one_arg._implementation
199+
with assert_raises_regex(AttributeError, '_implementation'):
204200
array.__array_function__(func=func, types=(np.ndarray,),
205201
args=(array,), kwargs={})
206202

@@ -385,9 +381,6 @@ def _(array):
385381

386382
assert_equal(np.sum(MyArray()), 'yes')
387383

388-
def test_sum_implementation_on_list(self):
389-
assert_equal(np.sum.__skip_array_function__([1, 2, 3]), 6)
390-
391384
def test_sum_on_mock_array(self):
392385

393386
# We need a proxy for mocks because __array_function__ is only looked
@@ -408,25 +401,16 @@ def __array__(self, *args, **kwargs):
408401
np.sum, (ArrayProxy,), (proxy,), {})
409402
proxy.value.__array__.assert_not_called()
410403

411-
proxy = ArrayProxy(mock.Mock(spec=ArrayProxy))
412-
proxy.value.__array__.return_value = np.array(2)
413-
result = np.sum.__skip_array_function__(proxy)
414-
assert_equal(result, 2)
415-
# TODO: switch to proxy.value.__array__.assert_called() and
416-
# proxy.value.__array_function__.assert_not_called() once we drop
417-
# Python 3.5 support.
418-
((called_method_name, _, _),) = proxy.value.mock_calls
419-
assert_equal(called_method_name, '__array__')
420-
421404
def test_sum_forwarding_implementation(self):
422405

423-
class MyArray(object):
406+
class MyArray(np.ndarray):
424407

425408
def sum(self, axis, out):
426409
return 'summed'
427410

428411
def __array_function__(self, func, types, args, kwargs):
429-
return func.__skip_array_function__(*args, **kwargs)
412+
return super().__array_function__(func, types, args, kwargs)
430413

431414
# note: the internal implementation of np.sum() calls the .sum() method
432-
assert_equal(np.sum(MyArray()), 'summed')
415+
array = np.array(1).view(MyArray)
416+
assert_equal(np.sum(array), 'summed')

0 commit comments

Comments
 (0)
0