Skip to content

Commit 3aab9cb

Browse files
committed
Parametrize Array with v2/v3 metadata
1 parent a0c56fb commit 3aab9cb

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

src/zarr/api/synchronous.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
)
4141
from zarr.core.dtype import ZDTypeLike
4242
from zarr.storage import StoreLike
43+
from zarr.types import AnyArray
4344

4445
__all__ = [
4546
"array",
@@ -168,7 +169,7 @@ def open(
168169
path: str | None = None,
169170
storage_options: dict[str, Any] | None = None,
170171
**kwargs: Any, # TODO: type kwargs as valid args to async_api.open
171-
) -> Array | Group:
172+
) -> AnyArray | Group:
172173
"""Open a group or array using file-mode-like semantics.
173174
174175
Parameters
@@ -365,7 +366,7 @@ def tree(grp: Group, expand: bool | None = None, level: int | None = None) -> An
365366

366367

367368
# TODO: add type annotations for kwargs
368-
def array(data: npt.ArrayLike | Array, **kwargs: Any) -> Array:
369+
def array(data: npt.ArrayLike | AnyArray, **kwargs: Any) -> AnyArray:
369370
"""Create an array filled with `data`.
370371
371372
Parameters
@@ -633,7 +634,7 @@ def create(
633634
storage_options: dict[str, Any] | None = None,
634635
config: ArrayConfigLike | None = None,
635636
**kwargs: Any,
636-
) -> Array:
637+
) -> AnyArray:
637638
"""Create an array.
638639
639640
Parameters
@@ -769,7 +770,7 @@ def create_array(
769770
overwrite: bool = False,
770771
config: ArrayConfigLike | None = None,
771772
write_data: bool = True,
772-
) -> Array:
773+
) -> AnyArray:
773774
"""Create an array.
774775
775776
This function wraps :func:`zarr.core.array.create_array`.
@@ -934,7 +935,7 @@ def from_array(
934935
storage_options: dict[str, Any] | None = None,
935936
overwrite: bool = False,
936937
config: ArrayConfigLike | None = None,
937-
) -> Array:
938+
) -> AnyArray:
938939
"""Create an array from an existing array or array-like.
939940
940941
Parameters
@@ -1128,7 +1129,7 @@ def from_array(
11281129

11291130

11301131
# TODO: add type annotations for kwargs
1131-
def empty(shape: ChunkCoords, **kwargs: Any) -> Array:
1132+
def empty(shape: ChunkCoords, **kwargs: Any) -> AnyArray:
11321133
"""Create an empty array with the specified shape. The contents will be filled with the
11331134
array's fill value or zeros if no fill value is provided.
11341135
@@ -1155,7 +1156,7 @@ def empty(shape: ChunkCoords, **kwargs: Any) -> Array:
11551156

11561157
# TODO: move ArrayLike to common module
11571158
# TODO: add type annotations for kwargs
1158-
def empty_like(a: ArrayLike, **kwargs: Any) -> Array:
1159+
def empty_like(a: ArrayLike, **kwargs: Any) -> AnyArray:
11591160
"""Create an empty array like another array. The contents will be filled with the
11601161
array's fill value or zeros if no fill value is provided.
11611162
@@ -1181,7 +1182,7 @@ def empty_like(a: ArrayLike, **kwargs: Any) -> Array:
11811182

11821183

11831184
# TODO: add type annotations for kwargs and fill_value
1184-
def full(shape: ChunkCoords, fill_value: Any, **kwargs: Any) -> Array:
1185+
def full(shape: ChunkCoords, fill_value: Any, **kwargs: Any) -> AnyArray:
11851186
"""Create an array with a default fill value.
11861187
11871188
Parameters
@@ -1203,7 +1204,7 @@ def full(shape: ChunkCoords, fill_value: Any, **kwargs: Any) -> Array:
12031204

12041205
# TODO: move ArrayLike to common module
12051206
# TODO: add type annotations for kwargs
1206-
def full_like(a: ArrayLike, **kwargs: Any) -> Array:
1207+
def full_like(a: ArrayLike, **kwargs: Any) -> AnyArray:
12071208
"""Create a filled array like another array.
12081209
12091210
Parameters
@@ -1222,7 +1223,7 @@ def full_like(a: ArrayLike, **kwargs: Any) -> Array:
12221223

12231224

12241225
# TODO: add type annotations for kwargs
1225-
def ones(shape: ChunkCoords, **kwargs: Any) -> Array:
1226+
def ones(shape: ChunkCoords, **kwargs: Any) -> AnyArray:
12261227
"""Create an array with a fill value of one.
12271228
12281229
Parameters
@@ -1241,7 +1242,7 @@ def ones(shape: ChunkCoords, **kwargs: Any) -> Array:
12411242

12421243

12431244
# TODO: add type annotations for kwargs
1244-
def ones_like(a: ArrayLike, **kwargs: Any) -> Array:
1245+
def ones_like(a: ArrayLike, **kwargs: Any) -> AnyArray:
12451246
"""Create an array of ones like another array.
12461247
12471248
Parameters
@@ -1267,7 +1268,7 @@ def open_array(
12671268
path: PathLike = "",
12681269
storage_options: dict[str, Any] | None = None,
12691270
**kwargs: Any,
1270-
) -> Array:
1271+
) -> AnyArray:
12711272
"""Open an array using file-mode-like semantics.
12721273
12731274
Parameters
@@ -1303,7 +1304,7 @@ def open_array(
13031304

13041305

13051306
# TODO: add type annotations for kwargs
1306-
def open_like(a: ArrayLike, path: str, **kwargs: Any) -> Array:
1307+
def open_like(a: ArrayLike, path: str, **kwargs: Any) -> AnyArray:
13071308
"""Open a persistent array like another array.
13081309
13091310
Parameters
@@ -1324,7 +1325,7 @@ def open_like(a: ArrayLike, path: str, **kwargs: Any) -> Array:
13241325

13251326

13261327
# TODO: add type annotations for kwargs
1327-
def zeros(shape: ChunkCoords, **kwargs: Any) -> Array:
1328+
def zeros(shape: ChunkCoords, **kwargs: Any) -> AnyArray:
13281329
"""Create an array with a fill value of zero.
13291330
13301331
Parameters
@@ -1343,7 +1344,7 @@ def zeros(shape: ChunkCoords, **kwargs: Any) -> Array:
13431344

13441345

13451346
# TODO: add type annotations for kwargs
1346-
def zeros_like(a: ArrayLike, **kwargs: Any) -> Array:
1347+
def zeros_like(a: ArrayLike, **kwargs: Any) -> AnyArray:
13471348
"""Create an array of zeros like another array.
13481349
13491350
Parameters

src/zarr/core/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,12 +1782,12 @@ def _info(
17821782

17831783
# TODO: Array can be a frozen data class again once property setters (e.g. shape) are removed
17841784
@dataclass(frozen=False)
1785-
class Array:
1785+
class Array(Generic[T_ArrayMetadata]):
17861786
"""
17871787
A Zarr array.
17881788
"""
17891789

1790-
_async_array: AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]
1790+
_async_array: AsyncArray[T_ArrayMetadata]
17911791

17921792
@classmethod
17931793
@deprecated("Use zarr.create_array instead.")

src/zarr/types.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import Any
2+
3+
from zarr.core.array import Array
4+
from zarr.core.metadata.v2 import ArrayV2Metadata
5+
from zarr.core.metadata.v3 import ArrayV3Metadata
6+
7+
AnyArray = Array[Any]
8+
AnyArray.__doc__ = "A Zarr format 2 or 3 `Array`"
9+
10+
ArrayV2 = Array[ArrayV2Metadata]
11+
ArrayV2.__doc__ = "A Zarr format 2 `Array`"
12+
13+
ArrayV3 = Array[ArrayV3Metadata]
14+
ArrayV3.__doc__ = "A Zarr format 3 `Array`"

tests/test_api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from zarr.abc.store import Store
1717
from zarr.core.common import JSON, MemoryOrder, ZarrFormat
18+
from zarr.types import AnyArray
1819

1920
import contextlib
2021
from typing import Literal
@@ -127,7 +128,7 @@ def test_write_empty_chunks_warns(write_empty_chunks: bool, zarr_format: ZarrFor
127128
def test_open_normalized_path(
128129
memory_store: MemoryStore, path: str, node_type: Literal["array", "group"]
129130
) -> None:
130-
node: Group | Array
131+
node: Group | AnyArray
131132
if node_type == "group":
132133
node = group(store=memory_store, path=path)
133134
elif node_type == "array":
@@ -1384,7 +1385,7 @@ def test_no_overwrite_load(tmp_path: Path) -> None:
13841385
zarr.zeros_like,
13851386
],
13861387
)
1387-
def test_auto_chunks(f: Callable[..., Array]) -> None:
1388+
def test_auto_chunks(f: Callable[..., AnyArray]) -> None:
13881389
# Make sure chunks are set automatically across the public API
13891390
# TODO: test shards with this test too
13901391
shape = (1000, 1000)

tests/test_array.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
from zarr.core.sync import sync
6666
from zarr.errors import ContainsArrayError, ContainsGroupError
6767
from zarr.storage import LocalStore, MemoryStore, StorePath
68+
from zarr.types import AnyArray
6869

6970
from .test_dtype.conftest import zdtype_examples
7071

@@ -1616,7 +1617,7 @@ async def test_from_array_arraylike(
16161617
store: Store,
16171618
chunks: Literal["auto", "keep"] | tuple[int, int],
16181619
write_data: bool,
1619-
src: Array | npt.ArrayLike,
1620+
src: AnyArray | npt.ArrayLike,
16201621
) -> None:
16211622
fill_value = 42
16221623
result = zarr.from_array(
@@ -1698,7 +1699,7 @@ def test_roundtrip_numcodecs() -> None:
16981699
assert metadata["codecs"] == expected
16991700

17001701

1701-
def _index_array(arr: Array, index: Any) -> Any:
1702+
def _index_array(arr: AnyArray, index: Any) -> Any:
17021703
return arr[index]
17031704

17041705

0 commit comments

Comments
 (0)