Skip to content

Commit 53da48a

Browse files
committed
Parametrize Array with v2/v3 metadata
1 parent a0c56fb commit 53da48a

File tree

14 files changed

+117
-88
lines changed

14 files changed

+117
-88
lines changed

src/zarr/api/asynchronous.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44
import dataclasses
55
import warnings
6-
from typing import TYPE_CHECKING, Any, Literal, cast
6+
from typing import TYPE_CHECKING, Any, Literal, TypeAlias, cast
77

88
import numpy as np
99
import numpy.typing as npt
@@ -52,9 +52,12 @@
5252
from zarr.core.buffer import NDArrayLikeOrScalar
5353
from zarr.core.chunk_key_encodings import ChunkKeyEncoding
5454
from zarr.storage import StoreLike
55+
from zarr.types import AnyArray
5556

5657
# TODO: this type could use some more thought
57-
ArrayLike = AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata] | Array | npt.NDArray[Any]
58+
ArrayLike: TypeAlias = (
59+
AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata] | AnyArray | npt.NDArray[Any]
60+
)
5861
PathLike = str
5962

6063
__all__ = [
@@ -562,7 +565,7 @@ async def tree(grp: AsyncGroup, expand: bool | None = None, level: int | None =
562565

563566

564567
async def array(
565-
data: npt.ArrayLike | Array, **kwargs: Any
568+
data: npt.ArrayLike | AnyArray, **kwargs: Any
566569
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
567570
"""Create an array filled with `data`.
568571

src/zarr/api/synchronous.py

Lines changed: 17 additions & 16 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`.
@@ -917,7 +918,7 @@ def create_array(
917918
def from_array(
918919
store: str | StoreLike,
919920
*,
920-
data: Array | npt.ArrayLike,
921+
data: AnyArray | npt.ArrayLike,
921922
write_data: bool = True,
922923
name: str | None = None,
923924
chunks: Literal["auto", "keep"] | ChunkCoords = "keep",
@@ -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: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
from zarr.core.dtype.wrapper import TBaseDType, TBaseScalar
139139
from zarr.core.group import AsyncGroup
140140
from zarr.storage import StoreLike
141+
from zarr.types import AnyArray
141142

142143

143144
# Array and AsyncArray are defined in the base ``zarr`` namespace
@@ -1782,12 +1783,12 @@ def _info(
17821783

17831784
# TODO: Array can be a frozen data class again once property setters (e.g. shape) are removed
17841785
@dataclass(frozen=False)
1785-
class Array:
1786+
class Array(Generic[T_ArrayMetadata]):
17861787
"""
17871788
A Zarr array.
17881789
"""
17891790

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

17921793
@classmethod
17931794
@deprecated("Use zarr.create_array instead.")
@@ -1820,7 +1821,7 @@ def create(
18201821
# runtime
18211822
overwrite: bool = False,
18221823
config: ArrayConfigLike | None = None,
1823-
) -> Array:
1824+
) -> AnyArray:
18241825
"""Creates a new Array instance from an initialized store.
18251826
18261827
.. deprecated:: 3.0.0
@@ -1949,7 +1950,7 @@ def _create(
19491950
# runtime
19501951
overwrite: bool = False,
19511952
config: ArrayConfigLike | None = None,
1952-
) -> Array:
1953+
) -> Self:
19531954
"""Creates a new Array instance from an initialized store.
19541955
See :func:`Array.create` for more details.
19551956
Deprecated in favor of :func:`zarr.create_array`.
@@ -1982,7 +1983,7 @@ def from_dict(
19821983
cls,
19831984
store_path: StorePath,
19841985
data: dict[str, JSON],
1985-
) -> Array:
1986+
) -> Self:
19861987
"""
19871988
Create a Zarr array from a dictionary.
19881989
@@ -2012,7 +2013,7 @@ def from_dict(
20122013
def open(
20132014
cls,
20142015
store: StoreLike,
2015-
) -> Array:
2016+
) -> AnyArray:
20162017
"""Opens an existing Array from a store.
20172018
20182019
Parameters
@@ -3681,7 +3682,7 @@ def append(self, data: npt.ArrayLike, axis: int = 0) -> ChunkCoords:
36813682
"""
36823683
return sync(self._async_array.append(data, axis=axis))
36833684

3684-
def update_attributes(self, new_attributes: dict[str, JSON]) -> Array:
3685+
def update_attributes(self, new_attributes: dict[str, JSON]) -> Self:
36853686
"""
36863687
Update the array's attributes.
36873688
@@ -3706,11 +3707,8 @@ def update_attributes(self, new_attributes: dict[str, JSON]) -> Array:
37063707
- The updated attributes will be merged with existing attributes, and any conflicts will be
37073708
overwritten by the new values.
37083709
"""
3709-
# TODO: remove this cast when type inference improves
37103710
new_array = sync(self._async_array.update_attributes(new_attributes))
3711-
# TODO: remove this cast when type inference improves
3712-
_new_array = cast("AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]", new_array)
3713-
return type(self)(_new_array)
3711+
return type(self)(new_array)
37143712

37153713
def __repr__(self) -> str:
37163714
return f"<Array {self.store_path} shape={self.shape} dtype={self.dtype}>"
@@ -3867,7 +3865,7 @@ class ShardsConfigParam(TypedDict):
38673865
async def from_array(
38683866
store: str | StoreLike,
38693867
*,
3870-
data: Array | npt.ArrayLike,
3868+
data: AnyArray | npt.ArrayLike,
38713869
write_data: bool = True,
38723870
name: str | None = None,
38733871
chunks: Literal["auto", "keep"] | ChunkCoords = "keep",
@@ -4103,7 +4101,9 @@ async def from_array(
41034101
if write_data:
41044102
if isinstance(data, Array):
41054103

4106-
async def _copy_array_region(chunk_coords: ChunkCoords | slice, _data: Array) -> None:
4104+
async def _copy_array_region(
4105+
chunk_coords: ChunkCoords | slice, _data: AnyArray
4106+
) -> None:
41074107
arr = await _data._async_array.getitem(chunk_coords)
41084108
await result.setitem(chunk_coords, arr)
41094109

@@ -4530,7 +4530,7 @@ async def create_array(
45304530

45314531

45324532
def _parse_keep_array_attr(
4533-
data: Array | npt.ArrayLike,
4533+
data: AnyArray | npt.ArrayLike,
45344534
chunks: Literal["auto", "keep"] | ChunkCoords,
45354535
shards: ShardsLike | None | Literal["keep"],
45364536
filters: FiltersLike | Literal["keep"],

src/zarr/core/attributes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
if TYPE_CHECKING:
99
from collections.abc import Iterator
1010

11-
from zarr.core.array import Array
1211
from zarr.core.group import Group
12+
from zarr.types import AnyArray
1313

1414

1515
class Attributes(MutableMapping[str, JSON]):
16-
def __init__(self, obj: Array | Group) -> None:
16+
def __init__(self, obj: AnyArray | Group) -> None:
1717
# key=".zattrs", read_only=False, cache=True, synchronizer=None
1818
self._obj = obj
1919

0 commit comments

Comments
 (0)