Skip to content

Commit 40df760

Browse files
committed
Fix shape recalculation bug for lazyexpr
1 parent fbbfcc9 commit 40df760

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/blosc2/lazyexpr.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,19 +2567,16 @@ def shape(self):
25672567
return None
25682568

25692569
# Operands shape can change, so we always need to recompute this
2570-
try:
2570+
if any(constructor in self.expression for constructor in constructors):
2571+
# might have an expression with pure constructors
2572+
opshapes = {k: v if not hasattr(v, "shape") else v.shape for k, v in self.operands.items()}
2573+
_shape = infer_shape(self.expression, opshapes) # infer shape, includes constructors
2574+
else:
25712575
_shape, chunks, blocks, fast_path = validate_inputs(self.operands, getattr(self, "_out", None))
25722576
if fast_path:
25732577
# fast_path ensure that all the operands have the same partitions
25742578
self._chunks = chunks
25752579
self._blocks = blocks
2576-
except ValueError as e:
2577-
if any(constructor in self.expression for constructor in constructors):
2578-
# might have an expression with pure constructors
2579-
opshapes = {k: v if not hasattr(v, "shape") else v.shape for k, v in self.operands.items()}
2580-
_shape = infer_shape(self.expression, opshapes) # infer shape, includes constructors
2581-
else:
2582-
raise e
25832580

25842581
self._shape_ = _shape
25852582
self._expression_ = self.expression

tests/ndarray/test_lazyexpr.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,21 @@ def test_broadcasting(broadcast_fixture):
952952
np.testing.assert_allclose(res, nres)
953953

954954

955+
def test_incompatible_shape():
956+
shape1 = (1000,)
957+
shape2 = (100,)
958+
a = blosc2.ones(shape1)
959+
b = blosc2.zeros(shape2)
960+
expr = a + b
961+
with pytest.raises(ValueError):
962+
s = expr.shape
963+
964+
# Test constructor too
965+
expr = a + blosc2.lazyexpr(f"linspace(0, 10, {np.prod(shape2)}, shape={shape2})")
966+
with pytest.raises(ValueError):
967+
s = expr.shape
968+
969+
955970
def test_broadcasting_str(broadcast_fixture):
956971
a1, a2, na1, na2 = broadcast_fixture
957972
expr1 = blosc2.lazyexpr("a1 + a2")

0 commit comments

Comments
 (0)