Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/awkward/operations/ak_from_buffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ def _from_buffer(
raise ValueError(
f"Mismatch of dtypes. Got {dtype}, but VirtualNDArray has {buffer.dtype}."
)
if count != buffer._shape[0]:
if (
count is not unknown_length
and buffer._shape[0] is not unknown_length
and count != buffer._shape[0]
):
raise ValueError(
f"Mismatch of lengths. Got {count}, but VirtualNDArray has {buffer._shape[0]}."
)
Expand Down
63 changes: 63 additions & 0 deletions tests/test_3667_virtual_array_shape_check_in_from_buffers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE

from __future__ import annotations

import numpy as np
import pytest

import awkward as ak
from awkward._nplikes.numpy import Numpy
from awkward._nplikes.shape import unknown_length
from awkward._nplikes.virtual import VirtualNDArray


@pytest.mark.parametrize("offsets_length", [5, unknown_length])
@pytest.mark.parametrize("content_length", [9, unknown_length])
def test(offsets_length, content_length):
offset_generator = lambda: np.array([0, 2, 4, 5, 6], dtype=np.int64) # noqa: E731
data_generator = lambda: np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.int64) # noqa: E731

offsets = ak.index.Index(
VirtualNDArray(
Numpy.instance(),
shape=(offsets_length,),
dtype=np.int64,
generator=offset_generator,
)
)
data = ak.contents.NumpyArray(
VirtualNDArray(
Numpy.instance(),
shape=(content_length,),
dtype=np.int64,
generator=data_generator,
)
)
array = ak.Array(ak.contents.ListOffsetArray(offsets, data))
assert array.to_list() == [[1, 2], [3, 4], [5], [6]]

buffers = {"node0-offsets": offset_generator, "node1-data": data_generator}
form = ak.forms.ListOffsetForm(
"i64", ak.forms.NumpyForm("int64", form_key="node1"), form_key="node0"
)
array = ak.from_buffers(form, 4, buffers)
assert array.to_list() == [[1, 2], [3, 4], [5], [6]]

offsets = VirtualNDArray(
Numpy.instance(),
shape=(offsets_length,),
dtype=np.int64,
generator=offset_generator,
)
data = VirtualNDArray(
Numpy.instance(),
shape=(content_length,),
dtype=np.int64,
generator=data_generator,
)
buffers = {"node0-offsets": offsets, "node1-data": data}
form = ak.forms.ListOffsetForm(
"i64", ak.forms.NumpyForm("int64", form_key="node1"), form_key="node0"
)
array = ak.from_buffers(form, 4, buffers)
assert array.to_list() == [[1, 2], [3, 4], [5], [6]]
Loading