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
7 changes: 6 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,16 @@ def manifest_array(array_v3_metadata):
def _manifest_array(
shape: tuple = (5, 2),
chunks: tuple = (5, 2),
data_type: np.dtype = np.dtype("int32"),
codecs: list[dict] | None = [ARRAYBYTES_CODEC, ZLIB_CODEC],
dimension_names: Iterable[str] | None = None,
):
metadata = array_v3_metadata(
shape=shape, chunks=chunks, codecs=codecs, dimension_names=dimension_names
shape=shape,
chunks=chunks,
data_type=data_type,
codecs=codecs,
dimension_names=dimension_names,
)
entries = _generate_chunk_entries(shape, chunks, _entry_from_chunk_key)
chunkmanifest = ChunkManifest(entries=entries)
Expand Down
18 changes: 14 additions & 4 deletions virtualizarr/manifests/array_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, Callable, cast
from typing import TYPE_CHECKING, Any, Callable, Union, cast

import numpy as np

Expand Down Expand Up @@ -34,12 +34,22 @@ def decorator(func):


@implements(np.result_type)
def result_type(*arrays_and_dtypes) -> np.dtype:
def result_type(*arrays_and_dtypes: Union["ManifestArray", np.dtype]) -> np.dtype:
"""Called by xarray to ensure all arguments to concat have the same dtype."""
first_dtype, *other_dtypes = (np.dtype(obj) for obj in arrays_and_dtypes)
from virtualizarr.manifests.array import ManifestArray

dtypes = (
obj.dtype if isinstance(obj, ManifestArray) else np.dtype(obj)
for obj in arrays_and_dtypes
)
first_dtype, *other_dtypes = dtypes
unique_dtypes = set(dtypes)
for other_dtype in other_dtypes:
if other_dtype != first_dtype:
raise ValueError("dtypes not all consistent")
raise ValueError(
f"Cannot combine arrays with inconsistent dtypes, but got {len(unique_dtypes)} distinct dtypes: {unique_dtypes}"
)

return first_dtype


Expand Down
19 changes: 18 additions & 1 deletion virtualizarr/tests/test_manifests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from virtualizarr.manifests import ChunkManifest, ManifestArray


class TestManifestArray:
class TestInit:
def test_manifest_array(self, array_v3_metadata):
chunks_dict = {
"0.0.0": {"path": "s3://bucket/foo.nc", "offset": 100, "length": 100},
Expand Down Expand Up @@ -50,6 +50,23 @@ def test_manifest_array_dict_v3_metadata(self, array_v3_metadata):
assert marr.ndim == 3


class TestResultType:
def test_idempotent(self, manifest_array):
marr1 = manifest_array(shape=(), chunks=(), data_type=np.dtype("int32"))
marr2 = manifest_array(shape=(), chunks=(), data_type=np.dtype("int32"))

assert np.result_type(marr1) == marr1.dtype
assert np.result_type(marr1, marr1.dtype) == marr1.dtype
assert np.result_type(marr1, marr2) == marr1.dtype

def test_raises(self, manifest_array):
marr1 = manifest_array(shape=(), chunks=(), data_type=np.dtype("int32"))
marr2 = manifest_array(shape=(), chunks=(), data_type=np.dtype("int64"))

with pytest.raises(ValueError, match="inconsistent"):
np.result_type(marr1, marr2)


class TestEquals:
def test_equals(self, array_v3_metadata):
chunks_dict = {
Expand Down
Loading