Skip to content

Commit

Permalink
fix: don't require mock and mock_empty together; use behaviors in…
Browse files Browse the repository at this point in the history
… empty array creation (#408)
  • Loading branch information
douglasdavis authored Nov 10, 2023
1 parent f447df4 commit a363cde
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/dask_awkward/layers/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ def io_func_implements_projection(func: ImplementsIOFunction) -> bool:


def io_func_implements_mocking(func: ImplementsIOFunction) -> bool:
return hasattr(func, "mock") and hasattr(func, "mock_empty")
return hasattr(func, "mock")


def io_func_implements_mock_empty(func: ImplementsIOFunction) -> bool:
return hasattr(func, "mock_empty")


def io_func_implements_columnar(func: ImplementsIOFunction) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion src/dask_awkward/lib/io/columnar.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def mock(self: S) -> AwkwardArray:

def mock_empty(self: S, backend: BackendT = "cpu") -> AwkwardArray:
return ak.to_backend(
self.form.length_zero_array(highlevel=False),
self.form.length_zero_array(highlevel=False, behavior=self.behavior),
backend,
highlevel=True,
)
Expand Down
3 changes: 3 additions & 0 deletions src/dask_awkward/lib/io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
BackendT,
ImplementsMocking,
IOFunctionWithMocking,
io_func_implements_mock_empty,
io_func_implements_mocking,
)
from dask_awkward.lib.core import (
Expand Down Expand Up @@ -622,6 +623,8 @@ def from_map(
raise ValueError("empty_on_raise and empty_backend must be used together.")

if empty_on_raise and empty_backend:
if not io_func_implements_mock_empty(io_func):
raise ValueError("io_func must implement mock_empty method.")
io_func = return_empty_on_raise(
io_func,
allowed_exceptions=empty_on_raise,
Expand Down
21 changes: 21 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,24 @@ def test_random_fail_from_lists():
label="from-lists",
empty_backend="cpu",
)

class NoMockEmpty:
def __init__(self, x):
self.x = x

def mock(self):
return 5

def __call__(self, *args):
return self.x * args[0]

with pytest.raises(ValueError, match="must implement"):
array = from_map(
NoMockEmpty(5),
many,
meta=typetracer_array(ak.Array(many[0])),
divisions=divs,
label="from-lists",
empty_on_raise=(RuntimeError,),
empty_backend="cpu",
)

0 comments on commit a363cde

Please sign in to comment.