Skip to content

Commit 41c1ebe

Browse files
committed
Fix not recalculating shape bug for lazyexprs
1 parent 935f54d commit 41c1ebe

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/blosc2/lazyexpr.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,19 +2573,17 @@ def shape(self):
25732573
return None
25742574

25752575
# Operands shape can change, so we always need to recompute this
2576-
try:
2576+
if any(constructor in self.expression for constructor in constructors):
2577+
# might have an expression with pure constructors
2578+
opshapes = {k: v if not hasattr(v, "shape") else v.shape for k, v in self.operands.items()}
2579+
_shape = infer_shape(self.expression, opshapes) # infer shape, includes constructors
2580+
2581+
else:
25772582
_shape, chunks, blocks, fast_path = validate_inputs(self.operands, getattr(self, "_out", None))
25782583
if fast_path:
25792584
# fast_path ensure that all the operands have the same partitions
25802585
self._chunks = chunks
25812586
self._blocks = blocks
2582-
except ValueError as e:
2583-
if any(constructor in self.expression for constructor in constructors):
2584-
# might have an expression with pure constructors
2585-
opshapes = {k: v if not hasattr(v, "shape") else v.shape for k, v in self.operands.items()}
2586-
_shape = infer_shape(self.expression, opshapes) # infer shape, includes constructors
2587-
else:
2588-
raise e
25892587

25902588
self._shape_ = _shape
25912589
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)