Skip to content

Commit 30479f2

Browse files
committed
Add an AnyAsyncArray type
1 parent 6b0e1cd commit 30479f2

File tree

6 files changed

+80
-127
lines changed

6 files changed

+80
-127
lines changed

src/zarr/api/asynchronous.py

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
GroupMetadata,
3939
create_hierarchy,
4040
)
41-
from zarr.core.metadata import ArrayMetadataDict, ArrayV2Metadata, ArrayV3Metadata
41+
from zarr.core.metadata import ArrayMetadataDict, ArrayV2Metadata
4242
from zarr.errors import GroupNotFoundError, NodeTypeValidationError
4343
from zarr.storage import StorePath
4444
from zarr.storage._common import make_store_path
@@ -52,12 +52,10 @@
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
55+
from zarr.types import AnyArray, AnyAsyncArray
5656

5757
# TODO: this type could use some more thought
58-
ArrayLike: TypeAlias = (
59-
AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata] | AnyArray | npt.NDArray[Any]
60-
)
58+
ArrayLike: TypeAlias = AnyAsyncArray | AnyArray | npt.NDArray[Any]
6159
PathLike = str
6260

6361
__all__ = [
@@ -309,7 +307,7 @@ async def open(
309307
path: str | None = None,
310308
storage_options: dict[str, Any] | None = None,
311309
**kwargs: Any, # TODO: type kwargs as valid args to open_array
312-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata] | AsyncGroup:
310+
) -> AnyAsyncArray | AsyncGroup:
313311
"""Convenience function to open a group or array using file-mode-like semantics.
314312
315313
Parameters
@@ -564,9 +562,7 @@ async def tree(grp: AsyncGroup, expand: bool | None = None, level: int | None =
564562
return await grp.tree(expand=expand, level=level)
565563

566564

567-
async def array(
568-
data: npt.ArrayLike | AnyArray, **kwargs: Any
569-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
565+
async def array(data: npt.ArrayLike | AnyArray, **kwargs: Any) -> AnyAsyncArray:
570566
"""Create an array filled with `data`.
571567
572568
Parameters
@@ -898,7 +894,7 @@ async def create(
898894
storage_options: dict[str, Any] | None = None,
899895
config: ArrayConfigLike | None = None,
900896
**kwargs: Any,
901-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
897+
) -> AnyAsyncArray:
902898
"""Create an array.
903899
904900
Parameters
@@ -1070,9 +1066,7 @@ async def create(
10701066
)
10711067

10721068

1073-
async def empty(
1074-
shape: ChunkCoords, **kwargs: Any
1075-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
1069+
async def empty(shape: ChunkCoords, **kwargs: Any) -> AnyAsyncArray:
10761070
"""Create an empty array with the specified shape. The contents will be filled with the
10771071
array's fill value or zeros if no fill value is provided.
10781072
@@ -1093,9 +1087,7 @@ async def empty(
10931087
return await create(shape=shape, fill_value=None, **kwargs)
10941088

10951089

1096-
async def empty_like(
1097-
a: ArrayLike, **kwargs: Any
1098-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
1090+
async def empty_like(a: ArrayLike, **kwargs: Any) -> AnyAsyncArray:
10991091
"""Create an empty array like `a`. The contents will be filled with the
11001092
array's fill value or zeros if no fill value is provided.
11011093
@@ -1122,9 +1114,7 @@ async def empty_like(
11221114

11231115

11241116
# TODO: add type annotations for fill_value and kwargs
1125-
async def full(
1126-
shape: ChunkCoords, fill_value: Any, **kwargs: Any
1127-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
1117+
async def full(shape: ChunkCoords, fill_value: Any, **kwargs: Any) -> AnyAsyncArray:
11281118
"""Create an array, with `fill_value` being used as the default value for
11291119
uninitialized portions of the array.
11301120
@@ -1146,9 +1136,7 @@ async def full(
11461136

11471137

11481138
# TODO: add type annotations for kwargs
1149-
async def full_like(
1150-
a: ArrayLike, **kwargs: Any
1151-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
1139+
async def full_like(a: ArrayLike, **kwargs: Any) -> AnyAsyncArray:
11521140
"""Create a filled array like `a`.
11531141
11541142
Parameters
@@ -1169,9 +1157,7 @@ async def full_like(
11691157
return await full(**like_kwargs)
11701158

11711159

1172-
async def ones(
1173-
shape: ChunkCoords, **kwargs: Any
1174-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
1160+
async def ones(shape: ChunkCoords, **kwargs: Any) -> AnyAsyncArray:
11751161
"""Create an array, with one being used as the default value for
11761162
uninitialized portions of the array.
11771163
@@ -1190,9 +1176,7 @@ async def ones(
11901176
return await create(shape=shape, fill_value=1, **kwargs)
11911177

11921178

1193-
async def ones_like(
1194-
a: ArrayLike, **kwargs: Any
1195-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
1179+
async def ones_like(a: ArrayLike, **kwargs: Any) -> AnyAsyncArray:
11961180
"""Create an array of ones like `a`.
11971181
11981182
Parameters
@@ -1219,7 +1203,7 @@ async def open_array(
12191203
path: PathLike = "",
12201204
storage_options: dict[str, Any] | None = None,
12211205
**kwargs: Any, # TODO: type kwargs as valid args to save
1222-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
1206+
) -> AnyAsyncArray:
12231207
"""Open an array using file-mode-like semantics.
12241208
12251209
Parameters
@@ -1267,9 +1251,7 @@ async def open_array(
12671251
raise
12681252

12691253

1270-
async def open_like(
1271-
a: ArrayLike, path: str, **kwargs: Any
1272-
) -> AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]:
1254+
async def open_like(a: ArrayLike, path: str, **kwargs: Any) -> AnyAsyncArray:
12731255
"""Open a persistent array like `a`.
12741256
12751257
Parameters
@@ -1292,9 +1274,7 @@ async def open_like(
12921274
return await open_array(path=path, **like_kwargs)
12931275

12941276

1295-
async def zeros(
1296-
shape: ChunkCoords, **kwargs: Any
1297-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
1277+
async def zeros(shape: ChunkCoords, **kwargs: Any) -> AnyAsyncArray:
12981278
"""Create an array, with zero being used as the default value for
12991279
uninitialized portions of the array.
13001280
@@ -1313,9 +1293,7 @@ async def zeros(
13131293
return await create(shape=shape, fill_value=0, **kwargs)
13141294

13151295

1316-
async def zeros_like(
1317-
a: ArrayLike, **kwargs: Any
1318-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
1296+
async def zeros_like(a: ArrayLike, **kwargs: Any) -> AnyAsyncArray:
13191297
"""Create an array of zeros like `a`.
13201298
13211299
Parameters

src/zarr/core/array.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
from zarr.core.dtype.wrapper import TBaseDType, TBaseScalar
141141
from zarr.core.group import AsyncGroup
142142
from zarr.storage import StoreLike
143-
from zarr.types import AnyArray
143+
from zarr.types import AnyArray, AnyAsyncArray
144144

145145

146146
# Array and AsyncArray are defined in the base ``zarr`` namespace
@@ -439,7 +439,7 @@ async def create(
439439
overwrite: bool = False,
440440
data: npt.ArrayLike | None = None,
441441
config: ArrayConfigLike | None = None,
442-
) -> AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]: ...
442+
) -> AnyAsyncArray: ...
443443

444444
@classmethod
445445
@deprecated("Use zarr.api.asynchronous.create_array instead.")
@@ -473,7 +473,7 @@ async def create(
473473
overwrite: bool = False,
474474
data: npt.ArrayLike | None = None,
475475
config: ArrayConfigLike | None = None,
476-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
476+
) -> AnyAsyncArray:
477477
"""Method to create a new asynchronous array instance.
478478
479479
.. deprecated:: 3.0.0
@@ -614,7 +614,7 @@ async def _create(
614614
overwrite: bool = False,
615615
data: npt.ArrayLike | None = None,
616616
config: ArrayConfigLike | None = None,
617-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
617+
) -> AnyAsyncArray:
618618
"""Method to create a new asynchronous array instance.
619619
See :func:`AsyncArray.create` for more details.
620620
Deprecated in favor of :func:`zarr.api.asynchronous.create_array`.
@@ -636,7 +636,7 @@ async def _create(
636636
_chunks = normalize_chunks(chunk_shape, shape, item_size)
637637
config_parsed = parse_array_config(config)
638638

639-
result: AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]
639+
result: AnyAsyncArray
640640
if zarr_format == 3:
641641
if dimension_separator is not None:
642642
raise ValueError(
@@ -905,7 +905,7 @@ def from_dict(
905905
cls,
906906
store_path: StorePath,
907907
data: dict[str, JSON],
908-
) -> AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]:
908+
) -> AnyAsyncArray:
909909
"""
910910
Create a Zarr array from a dictionary, with support for both Zarr format 2 and 3 metadata.
911911
@@ -937,7 +937,7 @@ async def open(
937937
cls,
938938
store: StoreLike,
939939
zarr_format: ZarrFormat | None = 3,
940-
) -> AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]:
940+
) -> AnyAsyncArray:
941941
"""
942942
Async method to open an existing Zarr array from a given store.
943943
@@ -3833,7 +3833,7 @@ def info_complete(self) -> Any:
38333833

38343834

38353835
async def chunks_initialized(
3836-
array: AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata],
3836+
array: AnyAsyncArray,
38373837
) -> tuple[str, ...]:
38383838
"""
38393839
Return the keys of the chunks that have been persisted to the storage backend.
@@ -3865,7 +3865,7 @@ async def chunks_initialized(
38653865

38663866

38673867
def _build_parents(
3868-
node: AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata] | AsyncGroup,
3868+
node: AnyAsyncArray | AsyncGroup,
38693869
) -> list[AsyncGroup]:
38703870
from zarr.core.group import AsyncGroup, GroupMetadata
38713871

@@ -3947,7 +3947,7 @@ async def from_array(
39473947
storage_options: dict[str, Any] | None = None,
39483948
overwrite: bool = False,
39493949
config: ArrayConfig | ArrayConfigLike | None = None,
3950-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
3950+
) -> AnyAsyncArray:
39513951
"""Create an array from an existing array or array-like.
39523952
39533953
Parameters
@@ -4210,7 +4210,7 @@ async def init_array(
42104210
dimension_names: DimensionNames = None,
42114211
overwrite: bool = False,
42124212
config: ArrayConfigLike | None,
4213-
) -> AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]:
4213+
) -> AnyAsyncArray:
42144214
"""Create and persist an array metadata document.
42154215
42164216
Parameters
@@ -4431,7 +4431,7 @@ async def create_array(
44314431
overwrite: bool = False,
44324432
config: ArrayConfigLike | None = None,
44334433
write_data: bool = True,
4434-
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
4434+
) -> AnyAsyncArray:
44354435
"""Create an array.
44364436
44374437
Parameters

0 commit comments

Comments
 (0)