Skip to content

Commit e99ffa1

Browse files
committed
Add test for objects following minimal Array protocol
1 parent d65ae78 commit e99ffa1

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

src/blosc2/lazyexpr.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,6 +3137,10 @@ def _new_expr(cls, expression, operands, guess, out=None, where=None, ne_args=No
31373137
_operands = operands | local_vars
31383138
_globals = get_expr_globals(expression)
31393139
_globals |= dtype_symbols
3140+
# Check that operands are proper Operands, LazyArray or scalars; if not, convert to NDArray objects
3141+
for op, val in _operands.items():
3142+
if not (isinstance(val, (blosc2.Operand, blosc2.LazyArray, np.ndarray)) or np.isscalar(val)):
3143+
_operands[op] = blosc2.asarray(val)
31403144
new_expr = eval(_expression, _globals, _operands)
31413145
_dtype = new_expr.dtype
31423146
_shape = new_expr.shape

src/blosc2/ndarray.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ def shape(self) -> tuple[int, ...]:
6060
"""The shape of the array."""
6161
...
6262

63-
@property
64-
def ndim(self) -> int:
65-
"""The number of dimensions of the array."""
66-
...
67-
6863
def __len__(self) -> int:
6964
"""The length of the array."""
7065
...

tests/ndarray/test_lazyexpr.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,3 +1555,30 @@ def test_complex_lazy_expression_multiplication():
15551555

15561556
# Also test getitem access
15571557
np.testing.assert_allclose(result_expr[:], -expected, rtol=1e-14, atol=1e-14)
1558+
1559+
1560+
# Test checking that objects following the blosc2.Array protocol can be operated with
1561+
def test_minimal_protocol():
1562+
class NewObj:
1563+
def __init__(self, a):
1564+
self.a = a
1565+
1566+
@property
1567+
def shape(self):
1568+
return self.a.shape
1569+
1570+
@property
1571+
def dtype(self):
1572+
return self.a.dtype
1573+
1574+
def __getitem__(self, key):
1575+
return self.a[key]
1576+
1577+
def __len__(self):
1578+
return len(self.a)
1579+
1580+
a = np.arange(100, dtype=np.int64).reshape(10, 10)
1581+
b = NewObj(a)
1582+
lb = blosc2.lazyexpr("b + 1")
1583+
1584+
np.testing.assert_array_equal(lb[:], a + 1)

0 commit comments

Comments
 (0)