3
3
import asyncio
4
4
import dataclasses
5
5
import warnings
6
- from typing import TYPE_CHECKING , Any , Literal , cast
6
+ from typing import TYPE_CHECKING , Any , Literal , NotRequired , TypedDict , cast
7
7
8
8
import numpy as np
9
9
import numpy .typing as npt
56
56
from zarr .abc .numcodec import Numcodec
57
57
from zarr .core .buffer import NDArrayLikeOrScalar
58
58
from zarr .core .chunk_key_encodings import ChunkKeyEncoding
59
+ from zarr .core .metadata .v2 import CompressorLikev2
59
60
from zarr .storage import StoreLike
60
61
61
62
# TODO: this type could use some more thought
@@ -124,10 +125,20 @@ def _get_shape_chunks(a: ArrayLike | Any) -> tuple[tuple[int, ...] | None, tuple
124
125
return shape , chunks
125
126
126
127
127
- def _like_args (a : ArrayLike , kwargs : dict [str , Any ]) -> dict [str , Any ]:
128
+ class _LikeArgs (TypedDict ):
129
+ shape : NotRequired [tuple [int , ...]]
130
+ chunks : NotRequired [tuple [int , ...]]
131
+ dtype : NotRequired [np .dtype [np .generic ]]
132
+ order : NotRequired [Literal ["C" , "F" ]]
133
+ filters : NotRequired [tuple [Numcodec , ...] | None ]
134
+ compressor : NotRequired [CompressorLikev2 ]
135
+ codecs : NotRequired [tuple [Codec , ...]]
136
+
137
+
138
+ def _like_args (a : ArrayLike ) -> _LikeArgs :
128
139
"""Set default values for shape and chunks if they are not present in the array-like object"""
129
140
130
- new = kwargs . copy ()
141
+ new : _LikeArgs = {}
131
142
132
143
shape , chunks = _get_shape_chunks (a )
133
144
if shape is not None :
@@ -138,9 +149,9 @@ def _like_args(a: ArrayLike, kwargs: dict[str, Any]) -> dict[str, Any]:
138
149
if hasattr (a , "dtype" ):
139
150
new ["dtype" ] = a .dtype
140
151
141
- if isinstance (a , AsyncArray ):
142
- new ["order" ] = a .order
152
+ if isinstance (a , AsyncArray | Array ):
143
153
if isinstance (a .metadata , ArrayV2Metadata ):
154
+ new ["order" ] = a .order
144
155
new ["compressor" ] = a .metadata .compressor
145
156
new ["filters" ] = a .metadata .filters
146
157
else :
@@ -1087,7 +1098,7 @@ async def empty(
1087
1098
shape : tuple [int , ...], ** kwargs : Any
1088
1099
) -> AsyncArray [ArrayV2Metadata ] | AsyncArray [ArrayV3Metadata ]:
1089
1100
"""Create an empty array with the specified shape. The contents will be filled with the
1090
- array's fill value or zeros if no fill value is provided.
1101
+ specified fill value or zeros if no fill value is provided.
1091
1102
1092
1103
Parameters
1093
1104
----------
@@ -1102,8 +1113,7 @@ async def empty(
1102
1113
retrieve data from an empty Zarr array, any values may be returned,
1103
1114
and these are not guaranteed to be stable from one access to the next.
1104
1115
"""
1105
-
1106
- return await create (shape = shape , fill_value = None , ** kwargs )
1116
+ return await create (shape = shape , ** kwargs )
1107
1117
1108
1118
1109
1119
async def empty_like (
@@ -1130,8 +1140,10 @@ async def empty_like(
1130
1140
retrieve data from an empty Zarr array, any values may be returned,
1131
1141
and these are not guaranteed to be stable from one access to the next.
1132
1142
"""
1133
- like_kwargs = _like_args (a , kwargs )
1134
- return await empty (** like_kwargs )
1143
+ like_kwargs = _like_args (a ) | kwargs
1144
+ if isinstance (a , (AsyncArray | Array )):
1145
+ like_kwargs .setdefault ("fill_value" , a .metadata .fill_value )
1146
+ return await empty (** like_kwargs ) # type: ignore[arg-type]
1135
1147
1136
1148
1137
1149
# TODO: add type annotations for fill_value and kwargs
@@ -1176,10 +1188,10 @@ async def full_like(
1176
1188
Array
1177
1189
The new array.
1178
1190
"""
1179
- like_kwargs = _like_args (a , kwargs )
1180
- if isinstance (a , AsyncArray ):
1191
+ like_kwargs = _like_args (a ) | kwargs
1192
+ if isinstance (a , ( AsyncArray | Array ) ):
1181
1193
like_kwargs .setdefault ("fill_value" , a .metadata .fill_value )
1182
- return await full (** like_kwargs )
1194
+ return await full (** like_kwargs ) # type: ignore[arg-type]
1183
1195
1184
1196
1185
1197
async def ones (
@@ -1220,8 +1232,8 @@ async def ones_like(
1220
1232
Array
1221
1233
The new array.
1222
1234
"""
1223
- like_kwargs = _like_args (a , kwargs )
1224
- return await ones (** like_kwargs )
1235
+ like_kwargs = _like_args (a ) | kwargs
1236
+ return await ones (** like_kwargs ) # type: ignore[arg-type]
1225
1237
1226
1238
1227
1239
async def open_array (
@@ -1300,10 +1312,10 @@ async def open_like(
1300
1312
AsyncArray
1301
1313
The opened array.
1302
1314
"""
1303
- like_kwargs = _like_args (a , kwargs )
1315
+ like_kwargs = _like_args (a ) | kwargs
1304
1316
if isinstance (a , (AsyncArray | Array )):
1305
- kwargs .setdefault ("fill_value" , a .metadata .fill_value )
1306
- return await open_array (path = path , ** like_kwargs )
1317
+ like_kwargs .setdefault ("fill_value" , a .metadata .fill_value )
1318
+ return await open_array (path = path , ** like_kwargs ) # type: ignore[arg-type]
1307
1319
1308
1320
1309
1321
async def zeros (
@@ -1344,5 +1356,5 @@ async def zeros_like(
1344
1356
Array
1345
1357
The new array.
1346
1358
"""
1347
- like_kwargs = _like_args (a , kwargs )
1348
- return await zeros (** like_kwargs )
1359
+ like_kwargs = _like_args (a ) | kwargs
1360
+ return await zeros (** like_kwargs ) # type: ignore[arg-type]
0 commit comments