diff options
author | Peter Andreas Entschev <peter@entschev.com> | 2020-11-16 10:19:39 -0800 |
---|---|---|
committer | Peter Andreas Entschev <peter@entschev.com> | 2020-11-16 10:19:39 -0800 |
commit | adc261a50b11310c97ffbcf57d6e20a5980bfed9 (patch) | |
tree | 0ac71fd30678263d9a240a70cba7f30297123a62 /numpy/core | |
parent | e0c9b265e171e92ee558c00712d1104e68f65c9b (diff) | |
download | numpy-adc261a50b11310c97ffbcf57d6e20a5980bfed9.tar.gz |
MAINT: Make like= in Python functions strict
Only allow objects that implement __array_function__
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/_asarray.py | 20 | ||||
-rw-r--r-- | numpy/core/numeric.py | 30 |
2 files changed, 40 insertions, 10 deletions
diff --git a/numpy/core/_asarray.py b/numpy/core/_asarray.py index a406308f3..ade53eeaf 100644 --- a/numpy/core/_asarray.py +++ b/numpy/core/_asarray.py @@ -5,6 +5,7 @@ Functions in the ``as*array`` family that promote array-likes into arrays. """ from .overrides import ( array_function_dispatch, + array_function_dispatch_like, set_array_function_like_doc, set_module, ) @@ -97,7 +98,9 @@ def asarray(a, dtype=None, order=None, *, like=None): """ if like is not None: - return _asarray_with_like(a, dtype=dtype, order=order, like=like) + return array_function_dispatch_like( + _asarray_with_like, a, dtype=dtype, order=order, like=like + ) return array(a, dtype, copy=False, order=order) @@ -166,7 +169,9 @@ def asanyarray(a, dtype=None, order=None, *, like=None): """ if like is not None: - return _asanyarray_with_like(a, dtype=dtype, order=order, like=like) + return array_function_dispatch_like( + _asanyarray_with_like, a, dtype=dtype, order=order, like=like + ) return array(a, dtype, copy=False, order=order, subok=True) @@ -223,7 +228,9 @@ def ascontiguousarray(a, dtype=None, *, like=None): """ if like is not None: - return _ascontiguousarray_with_like(a, dtype=dtype, like=like) + return array_function_dispatch_like( + _ascontiguousarray_with_like, a, dtype=dtype, like=like + ) return array(a, dtype, copy=False, order='C', ndmin=1) @@ -276,7 +283,9 @@ def asfortranarray(a, dtype=None, *, like=None): """ if like is not None: - return _asfortranarray_with_like(a, dtype=dtype, like=like) + return array_function_dispatch_like( + _asfortranarray_with_like, a, dtype=dtype, like=like + ) return array(a, dtype, copy=False, order='F', ndmin=1) @@ -363,7 +372,8 @@ def require(a, dtype=None, requirements=None, *, like=None): """ if like is not None: - return _require_with_like( + return array_function_dispatch_like( + _require_with_like, a, dtype=dtype, requirements=requirements, diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 25235f738..f0d1f163a 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -21,7 +21,9 @@ from .multiarray import ( from . import overrides from . import umath from . import shape_base -from .overrides import set_array_function_like_doc, set_module +from .overrides import ( + array_function_dispatch_like, set_array_function_like_doc, set_module + ) from .umath import (multiply, invert, sin, PINF, NAN) from . import numerictypes from .numerictypes import longlong, intc, int_, float_, complex_, bool_ @@ -198,7 +200,9 @@ def ones(shape, dtype=None, order='C', *, like=None): """ if like is not None: - return _ones_with_like(shape, dtype=dtype, order=order, like=like) + return array_function_dispatch_like( + _ones_with_like, shape, dtype=dtype, order=order, like=like + ) a = empty(shape, dtype, order) multiarray.copyto(a, 1, casting='unsafe') @@ -334,7 +338,14 @@ def full(shape, fill_value, dtype=None, order='C', *, like=None): """ if like is not None: - return _full_with_like(shape, fill_value, dtype=dtype, order=order, like=like) + return array_function_dispatch_like( + _full_with_like, + shape, + fill_value, + dtype=dtype, + order=order, + like=like, + ) if dtype is None: fill_value = asarray(fill_value) @@ -1847,7 +1858,14 @@ def fromfunction(function, shape, *, dtype=float, like=None, **kwargs): """ if like is not None: - return _fromfunction_with_like(function, shape, dtype=dtype, like=like, **kwargs) + return array_function_dispatch_like( + _fromfunction_with_like, + function, + shape, + dtype=dtype, + like=like, + **kwargs, + ) args = indices(shape, dtype=dtype) return function(*args, **kwargs) @@ -2168,7 +2186,9 @@ def identity(n, dtype=None, *, like=None): """ if like is not None: - return _identity_with_like(n, dtype=dtype, like=like) + return array_function_dispatch_like( + _identity_with_like, n, dtype=dtype, like=like + ) from numpy import eye return eye(n, dtype=dtype, like=like) |