Skip to content

Commit

Permalink
fix: use None when ndims can't be inferred from a layout-like obj (#3299
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pfackeldey authored Nov 11, 2024
1 parent 6c47e1d commit 549b6a4
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/awkward/_namedaxis.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,15 +652,15 @@ class NamedAxesWithDims:
"""

named_axis: list[AxisMapping]
ndims: list[int]
ndims: list[int | None]

def __post_init__(self):
if len(self.named_axis) != len(self.ndims):
raise ValueError(
"The number of dimensions must match the number of named axis mappings."
)

def __iter__(self) -> tp.Iterator[tuple[AxisMapping, int]]:
def __iter__(self) -> tp.Iterator[tuple[AxisMapping, int | None]]:
yield from zip(self.named_axis, self.ndims)

@classmethod
Expand All @@ -683,18 +683,20 @@ def prepare_contexts(
with HighLevelContext() as ctx:
layout = ctx.unwrap(array, **_unwrap_kwargs)
_named_axes.append(_get_named_axis(array))
_ndims.append(layout.minmax_depth[1])
_ndims.append(getattr(layout, "minmax_depth", (None, None))[1])

depth_context = {NAMED_AXIS_KEY: cls(_named_axes, _ndims)}
lateral_context = {NAMED_AXIS_KEY: cls(_named_axes, _ndims)}
return depth_context, lateral_context

def __setitem__(self, index: int, named_axis_with_ndim: tuple[AxisMapping, int]):
def __setitem__(
self, index: int, named_axis_with_ndim: tuple[AxisMapping, int | None]
):
named_axis, ndim = named_axis_with_ndim
self.named_axis[index] = named_axis
self.ndims[index] = ndim

def __getitem__(self, index: int) -> tuple[AxisMapping, int]:
def __getitem__(self, index: int) -> tuple[AxisMapping, int | None]:
return self.named_axis[index], self.ndims[index]

def __len__(self) -> int:
Expand Down

0 comments on commit 549b6a4

Please sign in to comment.