Skip to content

Commit 67a823d

Browse files
committed
add tests for synchronous create_array
1 parent 1e89ae2 commit 67a823d

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

src/zarr/api/synchronous.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
if TYPE_CHECKING:
1515
from collections.abc import Iterable
1616

17+
import numpy as np
1718
import numpy.typing as npt
1819

1920
from zarr.abc.codec import Codec
@@ -744,8 +745,9 @@ def create_array(
744745
store: str | StoreLike,
745746
*,
746747
name: str | None = None,
747-
shape: ShapeLike,
748-
dtype: npt.DTypeLike,
748+
shape: ShapeLike | None = None,
749+
dtype: npt.DTypeLike | None = None,
750+
data: np.ndarray[Any, np.dtype[Any]] | None = None,
749751
chunks: ChunkCoords | Literal["auto"] = "auto",
750752
shards: ShardsLike | None = None,
751753
filters: FiltersLike = "auto",
@@ -772,10 +774,13 @@ def create_array(
772774
name : str or None, optional
773775
The name of the array within the store. If ``name`` is ``None``, the array will be located
774776
at the root of the store.
775-
shape : ChunkCoords
776-
Shape of the array.
777-
dtype : npt.DTypeLike
778-
Data type of the array.
777+
shape : ChunkCoords, optional
778+
Shape of the array. Can be ``None`` if ``data`` is provided.
779+
dtype : npt.DTypeLike, optional
780+
Data type of the array. Can be ``None`` if ``data`` is provided.
781+
data : Array-like data to use for initializing the array. If this parameter is provided, the
782+
``shape`` and ``dtype`` parameters must be identical to ``data.shape`` and ``data.dtype``,
783+
or ``None``.
779784
chunks : ChunkCoords, optional
780785
Chunk shape of the array.
781786
If not specified, default are guessed based on the shape and dtype.
@@ -874,6 +879,7 @@ def create_array(
874879
name=name,
875880
shape=shape,
876881
dtype=dtype,
882+
data=data,
877883
chunks=chunks,
878884
shards=shards,
879885
filters=filters,

src/zarr/core/array.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4035,10 +4035,10 @@ async def create_array(
40354035
name : str or None, optional
40364036
The name of the array within the store. If ``name`` is ``None``, the array will be located
40374037
at the root of the store.
4038-
shape : ChunkCoords | None
4039-
Shape of the array.
4038+
shape : ChunkCoords, optional
4039+
Shape of the array. Can be ``None`` if ``data`` is provided.
40404040
dtype : npt.DTypeLike | None
4041-
Data type of the array.
4041+
Data type of the array. Can be ``None`` if ``data`` is provided.
40424042
data : Array-like data to use for initializing the array. If this parameter is provided, the
40434043
``shape`` and ``dtype`` parameters must be identical to ``data.shape`` and ``data.dtype``,
40444044
or ``None``.

tests/test_array.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pytest
1212

1313
import zarr.api.asynchronous
14+
import zarr.api.synchronous as sync_api
1415
from zarr import Array, AsyncArray, Group
1516
from zarr.abc.store import Store
1617
from zarr.codecs import (
@@ -38,13 +39,14 @@
3839
from zarr.core.common import JSON, MemoryOrder, ZarrFormat
3940
from zarr.core.group import AsyncGroup
4041
from zarr.core.indexing import BasicIndexer, ceildiv
41-
from zarr.core.metadata.v3 import DataType
42+
from zarr.core.metadata.v3 import ArrayV3Metadata, DataType
4243
from zarr.core.sync import sync
4344
from zarr.errors import ContainsArrayError, ContainsGroupError
4445
from zarr.storage import LocalStore, MemoryStore, StorePath
4546

4647
if TYPE_CHECKING:
4748
from zarr.core.array_spec import ArrayConfigLike
49+
from zarr.core.metadata.v2 import ArrayV2Metadata
4850

4951

5052
@pytest.mark.parametrize("store", ["local", "memory", "zip"], indirect=["store"])
@@ -1259,16 +1261,26 @@ async def test_create_array_v2_no_shards(store: MemoryStore) -> None:
12591261

12601262

12611263
@pytest.mark.parametrize("store", ["memory"], indirect=True)
1262-
async def test_create_array_data(store: Store) -> None:
1264+
@pytest.mark.parametrize("impl", ["sync", "async"])
1265+
async def test_create_array_data(impl: Literal["sync", "async"], store: Store) -> None:
12631266
"""
12641267
Test that we can invoke ``create_array`` with a ``data`` parameter.
12651268
"""
12661269
data = np.arange(10)
1267-
arr = await create_array(store, name="foo", data=data)
1268-
stored = await arr._get_selection(
1269-
BasicIndexer(..., shape=arr.shape, chunk_grid=arr.metadata.chunk_grid),
1270-
prototype=default_buffer_prototype(),
1271-
)
1270+
name = "foo"
1271+
arr: AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata] | Array
1272+
if impl == "sync":
1273+
arr = sync_api.create_array(store, name=name, data=data)
1274+
stored = arr[:]
1275+
elif impl == "async":
1276+
arr = await create_array(store, name=name, data=data, zarr_format=3)
1277+
stored = await arr._get_selection(
1278+
BasicIndexer(..., shape=arr.shape, chunk_grid=arr.metadata.chunk_grid),
1279+
prototype=default_buffer_prototype(),
1280+
)
1281+
else:
1282+
raise ValueError(f"Invalid impl: {impl}")
1283+
12721284
assert np.array_equal(stored, data)
12731285

12741286

0 commit comments

Comments
 (0)