Skip to content

Commit

Permalink
'behaviorof' should take Array, not layouts, and setting 'behavior' s…
Browse files Browse the repository at this point in the history
…hould set the '__class__'. (#1153)

* 'behaviorof' should take Array, not layouts, and setting 'behavior' should set the '__class__'.

* Setting 'behavior' to None should also change the class back to ak.Array.

* Fix ak.isclose, too.
  • Loading branch information
jpivarski committed Nov 17, 2021
1 parent 821b13d commit ccd7537
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 21 deletions.
12 changes: 3 additions & 9 deletions src/awkward/_v2/highlevel.py
Expand Up @@ -275,8 +275,6 @@ def __init__(
layout = ak._v2.operations.structure.with_name(
layout, with_name, highlevel=False
)
if self.__class__ is Array:
self.__class__ = ak._v2._util.arrayclass(layout, behavior)

if library is not None and library != ak._v2.operations.convert.library(layout):
layout = ak._v2.operations.convert.to_library(
Expand Down Expand Up @@ -367,6 +365,7 @@ def behavior(self):
@behavior.setter
def behavior(self, behavior):
if behavior is None or isinstance(behavior, Mapping):
self.__class__ = ak._v2._util.arrayclass(self._layout, behavior)
self._behavior = behavior
else:
raise TypeError("behavior must be None or a dict")
Expand Down Expand Up @@ -1447,8 +1446,6 @@ def __setstate__(self, state):
layout = ak._v2.operations.structure.concatenate(
layouts, highlevel=False
)
if self.__class__ is Array:
self.__class__ = ak._v2._util.arrayclass(layout, behavior)
self.layout = layout
self.behavior = behavior

Expand Down Expand Up @@ -1544,8 +1541,6 @@ def __init__(
layout = ak._v2.operations.structure.with_name(
layout, with_name, highlevel=False
)
if self.__class__ is Record:
self.__class__ = ak._v2._util.recordclass(layout, behavior)

if library is not None and library != ak._v2.operations.convert.library(layout):
layout = ak._v2.operations.convert.to_library(
Expand Down Expand Up @@ -1631,7 +1626,8 @@ def behavior(self):

@behavior.setter
def behavior(self, behavior):
if behavior is None or isinstance(behavior, dict):
if behavior is None or isinstance(behavior, Mapping):
self.__class__ = ak._v2._util.recordclass(self._layout, behavior)
self._behavior = behavior
else:
raise TypeError("behavior must be None or a dict")
Expand Down Expand Up @@ -2048,8 +2044,6 @@ def __setstate__(self, state):
layouts, highlevel=False
)
layout = ak._v2.record.Record(layout, at)
if self.__class__ is Record:
self.__class__ = ak._v2._util.recordclass(layout, behavior)
self.layout = layout
self.behavior = behavior

Expand Down
2 changes: 1 addition & 1 deletion src/awkward/_v2/operations/structure/ak_isclose.py
Expand Up @@ -46,7 +46,7 @@ def action(inputs, nplike, **kwargs):
),
)

behavior = ak._v2._util.behavior_of(one, two, behavior=behavior)
behavior = ak._v2._util.behavior_of(a, b, behavior=behavior)
out = ak._v2._broadcasting.broadcast_and_apply([one, two], action, behavior)
assert isinstance(out, tuple) and len(out) == 1

Expand Down
2 changes: 1 addition & 1 deletion src/awkward/_v2/operations/structure/ak_where.py
Expand Up @@ -104,7 +104,7 @@ def where(condition, *args, **kwargs):
# else:
# return None

# behavior = ak._v2._util.behaviorof(akcondition, left, right)
# behavior = ak._v2._util.behaviorof(condition, *args)
# out = ak._v2._util.broadcast_and_apply(
# [akcondition, left, right],
# getfunction,
Expand Down
10 changes: 2 additions & 8 deletions src/awkward/highlevel.py
Expand Up @@ -267,8 +267,6 @@ def __init__(
layout = ak.operations.structure.with_name(
layout, with_name, highlevel=False
)
if self.__class__ is Array:
self.__class__ = ak._util.arrayclass(layout, behavior)

if kernels is not None and kernels != ak.operations.convert.kernels(layout):
layout = ak.operations.convert.to_kernels(layout, kernels, highlevel=False)
Expand Down Expand Up @@ -360,6 +358,7 @@ def behavior(self):
@behavior.setter
def behavior(self, behavior):
if behavior is None or isinstance(behavior, dict):
self.__class__ = ak._util.arrayclass(self._layout, behavior)
self._behavior = behavior
else:
raise TypeError(
Expand Down Expand Up @@ -1480,8 +1479,6 @@ def __setstate__(self, state):
layout = ak.operations.convert.from_buffers(
form, length, container, highlevel=False, behavior=behavior
)
if self.__class__ is Array:
self.__class__ = ak._util.arrayclass(layout, behavior)
self.layout = layout
self.behavior = behavior
self._caches = ak._util.find_caches(self.layout)
Expand Down Expand Up @@ -1585,8 +1582,6 @@ def __init__(
layout = ak.operations.structure.with_name(
layout, with_name, highlevel=False
)
if self.__class__ is Record:
self.__class__ = ak._util.recordclass(layout, behavior)

if kernels is not None and kernels != ak.operations.convert.kernels(layout):
layout = ak.operations.convert.to_kernels(layout, kernels, highlevel=False)
Expand Down Expand Up @@ -1674,6 +1669,7 @@ def behavior(self):
@behavior.setter
def behavior(self, behavior):
if behavior is None or isinstance(behavior, dict):
self.__class__ = ak._util.recordclass(self._layout, behavior)
self._behavior = behavior
else:
raise TypeError(
Expand Down Expand Up @@ -2082,8 +2078,6 @@ def __setstate__(self, state):
form, length, container, highlevel=False, behavior=behavior
)
layout = ak.layout.Record(layout, at)
if self.__class__ is Record:
self.__class__ = ak._util.recordclass(layout, behavior)
self.layout = layout
self.behavior = behavior
self._caches = ak._util.find_caches(self.layout)
Expand Down
4 changes: 2 additions & 2 deletions src/awkward/operations/structure.py
Expand Up @@ -1744,7 +1744,7 @@ def getfunction(inputs):
else:
return None

behavior = ak._util.behaviorof(akcondition, left, right)
behavior = ak._util.behaviorof(condition, *args)
out = ak._util.broadcast_and_apply(
[akcondition, left, right],
getfunction,
Expand Down Expand Up @@ -4417,7 +4417,7 @@ def getfunction(inputs):
else:
return None

behavior = ak._util.behaviorof(one, two, behavior=behavior)
behavior = ak._util.behaviorof(a, b, behavior=behavior)
out = ak._util.broadcast_and_apply(
[one, two], getfunction, behavior, pass_depth=False
)
Expand Down

0 comments on commit ccd7537

Please sign in to comment.