Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: return proxy of attrs, behavior; dak.from_awkward correctly sets attrs #448

Merged
merged 9 commits into from
Jan 29, 2024
10 changes: 6 additions & 4 deletions src/dask_awkward/lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from functools import cached_property, partial, wraps
from inspect import getattr_static
from numbers import Number
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Literal, TypeVar, Union, overload

import awkward as ak
Expand Down Expand Up @@ -1090,17 +1091,18 @@ def layout(self) -> Content:
raise ValueError("This collection's meta is None; unknown layout.")

@property
def attrs(self) -> dict:
def attrs(self) -> Mapping[str, Any]:
"""awkward Array attrs dictionary."""
if self._meta is not None:
return self._meta.attrs
return MappingProxyType(self._meta.attrs)
raise ValueError("This collection's meta is None; no attrs property available.")

@property
def behavior(self) -> Mapping:
def behavior(self) -> Mapping | None:
"""awkward Array behavior dictionary."""
if self._meta is not None:
return self._meta.behavior
behavior = self._meta.behavior
return None if behavior is None else MappingProxyType(behavior)
raise ValueError(
"This collection's meta is None; no behavior property available."
)
Expand Down
6 changes: 3 additions & 3 deletions src/dask_awkward/lib/io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def __init__(
) -> None:
self.arr = arr
self.form = arr.layout.form
self.behavior = behavior
self.attrs = attrs
self.behavior = behavior if behavior else arr.behavior
self.attrs = attrs if attrs else arr.attrs

@property
def use_optimization(self):
Expand Down Expand Up @@ -119,7 +119,7 @@ def from_awkward(
return cast(
Array,
from_map(
FromAwkwardFn(source, behavior=behavior),
FromAwkwardFn(source, behavior=behavior, attrs=attrs),
starts_stops,
label=label or "from-awkward",
token=tokenize(source, npartitions),
Expand Down
18 changes: 18 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,24 @@ def test_shape_only_ops(fn: Callable, tmp_path_factory: pytest.TempPathFactory)
result.compute()


def test_assign_behavior() -> None:
x = ak.Array([{"a": 1, "b": 2}, {"a": 3, "b": 4}], behavior={}, attrs={})
dx = dak.from_awkward(x, 3)
lgray marked this conversation as resolved.
Show resolved Hide resolved
with pytest.raises(
TypeError, match="'mappingproxy' object does not support item assignment"
):
dx.behavior["should_fail"] = None # type: ignore


def test_assign_attrs() -> None:
x = ak.Array([{"a": 1, "b": 2}, {"a": 3, "b": 4}], behavior={}, attrs={})
dx = dak.from_awkward(x, 3)
with pytest.raises(
TypeError, match="'mappingproxy' object does not support item assignment"
):
dx.attrs["should_fail"] = None # type: ignore


@delayed
def a_delayed_array():
return ak.Array([2, 4])
Expand Down
Loading