8000 ENH: initial implementation of core `__array_function__` machinery by shoyer · Pull Request #12005 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: initial implementation of core __array_function__ machinery #12005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 24, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for ndarray.__array_function__
  • Loading branch information
shoyer committed Sep 21, 2018
commit 4ece3b1ce10e5aca966d8adfb85e6761747567b9
44 changes: 37 additions & 7 deletions numpy/core/tests/test_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def _get_overloaded_args(relevant_args):
return args


_IMPLEMENTED = None
def _return_self(self, *args, **kwargs):
return self


class TestGetOverloadedTypesAndArgs(object):
Expand Down Expand Up @@ -42,7 +43,7 @@ def test_ndarray(self):
def test_ndarray_subclasses(self):

class OverrideSub(np.ndarray):
__array_function__ = _IMPLEMENTED
__array_function__ = _return_self

class NoOverrideSub(np.ndarray):
pass
Expand All @@ -67,7 +68,7 @@ class NoOverrideSub(np.ndarray):
def test_ndarray_and_duck_array(self):

class Other(object):
__array_function__ = _IMPLEMENTED
__array_function__ = _return_self

array = np.array(1)
other = Other()
Expand All @@ -83,16 +84,16 @@ class Other(object):
def test_many_duck_arrays(self):

class A(object):
__array_function__ = _IMPLEMENTED
__array_function__ = _return_self

class B(A):
__array_function__ = _IMPLEMENTED
__array_function__ = _return_self

class C(A):
__array_function__ = _IMPLEMENTED
__array_function__ = _return_self

class D(object):
__array_function__ = _IMPLEMENTED
__array_function__ = _return_self

a = A()
b = B()
Expand All @@ -110,6 +111,34 @@ class D(object):
assert_equal(_get_overloaded_args([a, c, b]), [c, b, a])


class TestNDArrayArrayFunction(object):

def test_method(self):

class SubOverride(np.ndarray):
__array_function__ = _return_self

class NoOverrideSub(np.ndarray):
pass

array = np.array(1)

def func():
return 'original'

result = array.__array_function__(
func=func, types=(np.ndarray,), args=(), kwargs={})
assert_equal(result, 'original')

result = array.__array_function__(
func=func, types=(np.ndarray, SubOverride), args=(), kwargs={})
assert_(result is NotImplemented)

result = array.__array_function__(
func=func, types=(np.ndarray, NoOverrideSub), args=(), kwargs={})
assert_equal(result, 'original')


# need to define this at the top level to test pickling
@array_function_dispatch(lambda array: (array,))
def dispatched_one_arg(array):
Expand All @@ -128,6 +157,7 @@ def test_docstring(self):


def _new_duck_type_and_implements():
"""Create a duck array type and implements functions."""
HANDLED_FUNCTIONS = {}

class MyArray(object):
Expand Down
0