Skip to content

Commit d50676b

Browse files
committed
Raise error when a lazyexpr misses an operand
1 parent 2996ad4 commit d50676b

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

src/blosc2/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
class MissingOperands(ValueError):
3+
def __init__(self, expr, missing_ops):
4+
self.expr = expr
5+
self.missing_ops = missing_ops
6+
7+
message = f"Lazy expression \"{expr}\" with missing operands: {missing_ops}"
8+
super().__init__(message)

src/blosc2/lazyexpr.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
from numpy.exceptions import ComplexWarning
3131

32+
from . import exceptions
33+
3234
if TYPE_CHECKING:
3335
from collections.abc import Callable, Sequence
3436

@@ -3294,14 +3296,16 @@ def _open_lazyarray(array):
32943296
operands = lazyarray["operands"]
32953297
parent_path = Path(array.schunk.urlpath).parent
32963298
operands_dict = {}
3299+
missing_ops = {}
32973300
for key, value in operands.items():
32983301
if isinstance(value, str):
32993302
value = parent_path / value
33003303
try:
33013304
op = blosc2.open(value)
33023305
except FileNotFoundError:
3303-
op = None
3304-
operands_dict[key] = op
3306+
missing_ops[key] = value
3307+
else:
3308+
operands_dict[key] = op
33053309
elif isinstance(value, dict):
33063310
# C2Array
33073311
operands_dict[key] = blosc2.C2Array(
@@ -3312,16 +3316,17 @@ def _open_lazyarray(array):
33123316
raise TypeError("Error when retrieving the operands")
33133317

33143318
expr = lazyarray["expression"]
3315-
try:
3316-
new_expr = LazyExpr._new_expr(expr, operands_dict, guess=True, out=None, where=None)
3317-
# Make the array info available for the user (only available when opened from disk)
3318-
new_expr.array = array
3319-
# We want to expose schunk too, so that .info() can be used on the LazyArray
3320-
new_expr.schunk = array.schunk
3321-
except RuntimeError:
3322-
# Something unexpected happened. Avoid guessing and return something that
3323-
# can be introspected.
3324-
new_expr = LazyExpr._new_expr(expr, operands_dict, guess=False, out=None, where=None)
3319+
if missing_ops:
3320+
exc = exceptions.MissingOperands(expr, missing_ops)
3321+
exc.expr = expr
3322+
exc.missing_ops = missing_ops
3323+
raise exc
3324+
3325+
new_expr = LazyExpr._new_expr(expr, operands_dict, guess=True, out=None, where=None)
3326+
# Make the array info available for the user (only available when opened from disk)
3327+
new_expr.array = array
3328+
# We want to expose schunk too, so that .info() can be used on the LazyArray
3329+
new_expr.schunk = array.schunk
33253330
return new_expr
33263331

33273332

tests/ndarray/test_lazyexpr.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# LICENSE file in the root directory of this source tree)
77
#######################################################################
88
import math
9+
import pathlib
910

1011
import numpy as np
1112
import pytest
@@ -1344,17 +1345,14 @@ def test_missing_operator():
13441345
# Remove the file for operand b
13451346
blosc2.remove_urlpath("b.b2nd")
13461347
# Re-open the lazy expression
1347-
expr2 = blosc2.open("expr.b2nd")
1348-
# Check that some operand is missing
1349-
assert expr2.operands["a"] is not None
1350-
assert expr2.operands["b"] is None
1351-
# Check that the expression is still there, and can be introspected
1352-
# Note the added parentheses. The parser automatically adds these,
1353-
# mainly because of possible operator precedence issues in nested expressions.
1354-
assert expr2.expression == "a + b"
1355-
# Check that dtype and shape are None
1356-
assert expr2.dtype is None
1357-
assert expr2.shape is None
1348+
with pytest.raises(blosc2.exceptions.MissingOperands) as excinfo:
1349+
blosc2.open("expr.b2nd")
1350+
1351+
# Check that some operand is missing"
1352+
assert "a" not in excinfo.value.missing_ops
1353+
assert excinfo.value.missing_ops["b"] == pathlib.Path("b.b2nd")
1354+
assert excinfo.value.expr == "a + b"
1355+
13581356
# Clean up
13591357
blosc2.remove_urlpath("a.b2nd")
13601358
blosc2.remove_urlpath("expr.b2nd")

0 commit comments

Comments
 (0)