Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fixed ArrayV2Metadata parameter names #2270

Closed
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
4 changes: 2 additions & 2 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ async def _create_v2(

metadata = ArrayV2Metadata(
shape=shape,
dtype=np.dtype(dtype),
chunks=chunks,
data_type=np.dtype(dtype),
chunk_grid=chunks,
order=order,
dimension_separator=dimension_separator,
fill_value=0 if fill_value is None else fill_value,
Expand Down
8 changes: 7 additions & 1 deletion src/zarr/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
if TYPE_CHECKING:
from collections.abc import Awaitable, Callable, Iterator

from zarr.core.chunk_grids import RegularChunkGrid


ZARR_JSON = "zarr.json"
ZARRAY_JSON = ".zarray"
Expand Down Expand Up @@ -133,11 +135,15 @@ def parse_named_configuration(
return name_parsed, configuration_parsed


def parse_shapelike(data: int | Iterable[int]) -> tuple[int, ...]:
def parse_shapelike(data: int | Iterable[int] | RegularChunkGrid) -> tuple[int, ...]:
from zarr.core.chunk_grids import RegularChunkGrid

if isinstance(data, int):
if data < 0:
raise ValueError(f"Expected a non-negative integer. Got {data} instead")
return (data,)
elif isinstance(data, RegularChunkGrid):
return data.chunk_shape
try:
data_tuple = tuple(data)
except TypeError as e:
Expand Down
19 changes: 11 additions & 8 deletions src/zarr/core/metadata/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def __init__(
self,
*,
shape: ChunkCoords,
dtype: npt.DTypeLike,
chunks: ChunkCoords,
data_type: npt.DTypeLike,
chunk_grid: ChunkCoords,
fill_value: Any,
order: Literal["C", "F"],
dimension_separator: Literal[".", "/"] = ".",
Expand All @@ -57,8 +57,8 @@ def __init__(
Metadata for a Zarr version 2 array.
"""
shape_parsed = parse_shapelike(shape)
data_type_parsed = parse_dtype(dtype)
chunks_parsed = parse_shapelike(chunks)
data_type_parsed = parse_dtype(data_type)
chunks_parsed = parse_shapelike(chunk_grid)
compressor_parsed = parse_compressor(compressor)
order_parsed = parse_indexing_order(order)
dimension_separator_parsed = parse_separator(dimension_separator)
Expand Down Expand Up @@ -141,7 +141,10 @@ def from_dict(cls, data: dict[str, Any]) -> ArrayV2Metadata:
_data = data.copy()
# check that the zarr_format attribute is correct
_ = parse_zarr_format(_data.pop("zarr_format"))
dtype = parse_dtype(_data["dtype"])

_data["chunk_grid"] = _data.pop("chunks")
_data["data_type"] = _data.pop("dtype")
dtype = parse_dtype(_data["data_type"])

if dtype.kind in "SV":
fill_value_encoded = _data.get("fill_value")
Expand All @@ -153,9 +156,9 @@ def from_dict(cls, data: dict[str, Any]) -> ArrayV2Metadata:
# We don't want the ArrayV2Metadata constructor to fail just because someone put an
# extra key in the metadata.
expected = {x.name for x in fields(cls)}
# https://github.com/zarr-developers/zarr-python/issues/2269
# handle the renames
expected |= {"dtype", "chunks"}
# # https://github.com/zarr-developers/zarr-python/issues/2269
# # handle the renames
# expected |= {"dtype", "chunks"}

_data = {k: v for k, v in _data.items() if k in expected}

Expand Down
4 changes: 2 additions & 2 deletions tests/v3/test_metadata/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def test_from_dict_extra_fields() -> None:
expected = ArrayV2Metadata(
attributes={"key": "value"},
shape=(8,),
dtype="float64",
chunks=(8,),
data_type="float64",
chunk_grid=(8,),
fill_value=0.0,
order="C",
)
Expand Down
13 changes: 13 additions & 0 deletions tests/v3/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ def test_codec_pipeline() -> None:
np.testing.assert_array_equal(result, expected)


def test_attrs(store: StorePath) -> None:
data = np.arange(0, 8, dtype="uint16")
a = Array.create(
store / "simple_v2",
zarr_format=2,
shape=data.shape,
chunks=(4,),
dtype=data.dtype,
fill_value=0,
)
a.attrs.put({"key": 0})


@pytest.mark.parametrize("dtype", ["|S", "|V"])
async def test_v2_encode_decode(dtype):
store = zarr.storage.MemoryStore(mode="w")
Expand Down