Skip to content

Commit

Permalink
Fix attribute access for __array_function__. (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
hameerabbasi authored Jan 18, 2020
1 parent 9e1213a commit b4bf66d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
17 changes: 14 additions & 3 deletions sparse/_sparse_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def __array__(self, **kwargs):
def __array_function__(self, func, types, args, kwargs):
import sparse as module

sparse_func = None
try:
submodules = getattr(func, "__module__", "numpy").split(".")[1:]
for submodule in submodules:
Expand All @@ -242,9 +243,19 @@ def __array_function__(self, func, types, args, kwargs):
try:
sparse_func = getattr(type(self), func.__name__)
except AttributeError:
return NotImplemented
pass

if not isinstance(sparse_func, Callable):
return getattr(self, func.__name__)
if (
not isinstance(sparse_func, Callable)
and len(args) == 1
and len(kwargs) == 0
):
try:
return getattr(self, func.__name__)
except AttributeError:
pass

if sparse_func is None:
return NotImplemented

return sparse_func(*args, **kwargs)
9 changes: 9 additions & 0 deletions sparse/tests/test_array_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,12 @@ def test_ternary(func, arg_order):
args = [(x, y)[i] for i in arg_order]
yy = func(*args)
assert_eq(xx, yy)


@pytest.mark.parametrize("func", [np.shape, np.size, np.ndim])
def test_property(func):
y = sparse.random((50, 50), density=0.25)
x = y.todense()
xx = func(x)
yy = func(y)
assert xx == yy

0 comments on commit b4bf66d

Please sign in to comment.