Skip to content

Commit

Permalink
Guard against shape attributes that are not sequences
Browse files Browse the repository at this point in the history
Fixes dask#1151
  • Loading branch information
mrocklin committed May 11, 2016
1 parent 472ce70 commit cdd955e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions dask/array/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,7 @@ def asarray(array):
"""
if not isinstance(array, Array):
name = 'asarray-' + tokenize(array)
if not hasattr(array, 'shape'):
if isinstance(getattr(array, 'shape', None), Iterable):
array = np.asarray(array)
array = from_array(array, chunks=array.shape, name=name)
return array
Expand Down Expand Up @@ -2103,7 +2103,7 @@ def is_scalar_for_elemwise(arg):
True
"""
return (np.isscalar(arg)
or not hasattr(arg, 'shape')
or not isinstance(getattr(arg, 'shape', None), Iterable)
or isinstance(arg, np.dtype)
or (isinstance(arg, np.ndarray) and arg.ndim == 0))

Expand Down Expand Up @@ -2156,6 +2156,7 @@ def elemwise(op, *args, **kwargs):
(op.__name__, str(sorted(set(kwargs) - set(['name', 'dtype'])))))

shapes = [getattr(arg, 'shape', ()) for arg in args]
shapes = [s if isinstance(s, Iterable) else () for s in shapes]
out_ndim = len(broadcast_shapes(*shapes)) # Raises ValueError if dimensions mismatch
expr_inds = tuple(range(out_ndim))[::-1]

Expand Down
6 changes: 6 additions & 0 deletions dask/array/tests/test_array_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2010,3 +2010,9 @@ def test_copy():
def test_npartitions():
assert da.ones(5, chunks=(2,)).npartitions == 3
assert da.ones((5, 5), chunks=(2, 3)).npartitions == 6


def test_astype_gh1151():
a = np.arange(5).astype(np.int32)
b = da.from_array(a, (1,))
assert_eq(a.astype(np.int16), b.astype(np.int16))

0 comments on commit cdd955e

Please sign in to comment.