Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle unhashable behaviour type #2770

Merged
merged 11 commits into from
Oct 26, 2023
12 changes: 10 additions & 2 deletions src/awkward/_connect/numba/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,16 @@ def box_ArrayBuilder(arraybuildertype, arraybuilderval, c):
ArrayBuilder_obj = c.pyapi.unserialize(
c.pyapi.serialize_object(ak.highlevel.ArrayBuilder)
)
behavior_obj = c.pyapi.unserialize(
c.pyapi.serialize_object(arraybuildertype.behavior)
serializable2dict_obj = c.pyapi.unserialize(
c.pyapi.serialize_object(ak._connect.numba.arrayview.serializable2dict)
)
behavior2_obj = c.pyapi.unserialize(
c.pyapi.serialize_object(
ak._connect.numba.arrayview.dict2serializable(arraybuildertype.behavior)
)
)
behavior_obj = c.pyapi.call_function_objargs(
serializable2dict_obj, (behavior2_obj,)
)

proxyin = c.context.make_helper(c.builder, arraybuildertype, arraybuilderval)
Expand Down
10 changes: 10 additions & 0 deletions src/awkward/_connect/numba/layoutbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@ def getter(builder):
return getter


@numba.extending.overload_method(EmptyType, "append")
def Empty_append(builder, datum):
if isinstance(builder, EmptyType):

def append(builder, datum):
raise NumbaTypeError("Empty cannot append data")

return append


########## ListOffset #########################################################


Expand Down
4 changes: 3 additions & 1 deletion tests/test_2408_layoutbuilder_in_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

numba = pytest.importorskip("numba")

from numba.core.errors import NumbaTypeError # noqa: E402

import awkward.numba.layoutbuilder as lb # noqa: E402

ak.numba.register_and_check()
Expand Down Expand Up @@ -593,7 +595,7 @@ def f2(x):

builder = lb.Empty()
# Unknown attribute 'append' of type lb.Empty
with pytest.raises(numba.core.errors.TypingError):
with pytest.raises(NumbaTypeError):
f2(builder)


Expand Down
40 changes: 40 additions & 0 deletions tests/test_2770_serialize_and_deserialize_behaviour_for_numba.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE

import numpy as np
import pytest

import awkward as ak

numba = pytest.importorskip("numba")


def test_ArrayBuilder_behavior():
SOME_ATTRS = {"FOO": "BAR"}
builder = ak.ArrayBuilder(behavior=SOME_ATTRS)

@numba.njit
def func(array):
return array

assert builder.behavior is SOME_ATTRS
assert func(builder).behavior is SOME_ATTRS

def make_add_xyr():
def add_xyr(left, right):
x = left.x + right.x
y = left.y + right.y
return ak.zip(
{
"x": x,
"y": y,
"r": np.sqrt(x**2 + y**2),
},
with_name="xyr",
)

return add_xyr

behavior = {(np.add, "xyr", "xyr"): make_add_xyr()}

builder = ak.ArrayBuilder(behavior=behavior)
assert func(builder).behavior is behavior