@@ -148,11 +148,18 @@ class _LikeArgs(TypedDict):
148148 compressor : NotRequired [CompressorLikev2 ]
149149 codecs : NotRequired [tuple [Codec , ...]]
150150 fill_value : NotRequired [Any ]
151+ zarr_format : NotRequired [ZarrFormat ]
151152
152153
153- def _like_args (a : ArrayLike ) -> _LikeArgs :
154- """Set default values for shape and chunks if they are not present in the array-like object"""
154+ def _like_args (a : ArrayLike , zarr_format : ZarrFormat | None ) -> _LikeArgs :
155+ """
156+ Arguments for creating an array like `a` in `zarr_format`.
155157
158+ If `a` is a zarr array, the new array has the zarr format of `a` unless `zarr_format`
159+ requests another. The storage settings of `a` (memory order and codecs) are specific to
160+ its zarr format, so they are copied only when the new array has the format of `a`.
161+ Otherwise the new array uses the defaults of its own format.
162+ """
156163 new : _LikeArgs = {}
157164
158165 shape , chunks = _get_shape_chunks (a )
@@ -166,21 +173,27 @@ def _like_args(a: ArrayLike) -> _LikeArgs:
166173
167174 if isinstance (a , AsyncArray | Array ):
168175 new ["fill_value" ] = a .metadata .fill_value
169- if isinstance (a .metadata , ArrayV2Metadata ):
170- new ["order" ] = a .order
171- new ["compressor" ] = a .metadata .compressor
172- new ["filters" ] = a .metadata .filters
173- else :
174- # TODO: Remove type: ignore statement when type inference improves.
175- # mypy cannot correctly infer the type of a.metadata here for some reason.
176- new ["codecs" ] = a .metadata .codecs
176+ if zarr_format is None :
177+ zarr_format = a .metadata .zarr_format
178+ if a .metadata .zarr_format == zarr_format :
179+ if isinstance (a .metadata , ArrayV2Metadata ):
180+ new ["order" ] = a .order
181+ new ["compressor" ] = a .metadata .compressor
182+ new ["filters" ] = a .metadata .filters
183+ else :
184+ # TODO: Remove type: ignore statement when type inference improves.
185+ # mypy cannot correctly infer the type of a.metadata here for some reason.
186+ new ["codecs" ] = a .metadata .codecs
177187
178188 else :
179189 # TODO: set default values compressor/codecs
180190 # to do this, we may need to evaluate if this is a v2 or v3 array
181191 # new["compressor"] = "default"
182192 pass
183193
194+ if zarr_format is not None :
195+ new ["zarr_format" ] = zarr_format
196+
184197 return new
185198
186199
@@ -922,7 +935,8 @@ async def create(
922935 dimension_names : DimensionNamesLike = None ,
923936 storage_options : dict [str , Any ] | None = None ,
924937 config : ArrayConfigLike | None = None ,
925- ** kwargs : Any ,
938+ mode : AccessModeLiteral | None = None ,
939+ data : npt .ArrayLike | None = None ,
926940) -> AnyAsyncArray :
927941 """Create an array.
928942
@@ -1042,6 +1056,26 @@ async def create(
10421056 config : ArrayConfigLike, optional
10431057 Runtime configuration of the array. If provided, will override the
10441058 default values from `zarr.config.array`.
1059+ mode : {'r', 'r+', 'a', 'w', 'w-'}, optional
1060+ Legacy way to control overwriting, kept for compatibility with Zarr-Python 2.
1061+ Prefer `overwrite`. The access mode used to open `store`; the default, `None`,
1062+ is `'a'`.
1063+
1064+ - `'a'` and `'r+'` create the array and fail if a node exists at `path`,
1065+ unless `overwrite` is `True`.
1066+ - `'w'` replaces anything stored under `path` (the whole store if `path` is
1067+ not set), even if `overwrite` is `False`. On a store that cannot delete
1068+ keys, `'w'` raises an error instead of replacing an existing node.
1069+ - `'w-'` fails if anything is stored under `path`, even if `overwrite` is
1070+ `True`.
1071+ - `'r'` always fails.
1072+
1073+ `mode` has no effect, and is not validated, if `store` is a `StorePath`.
1074+ data : array-like, optional
1075+ Values written into the new array after it is created. Unlike the `data`
1076+ parameter of `create_array`, it does not set the shape or data type of the
1077+ array. To create an array from existing data, use `create_array(data=...)`.
1078+ A Zarr array is not supported as `data`.
10451079
10461080 Returns
10471081 -------
@@ -1066,7 +1100,6 @@ async def create(
10661100 if write_empty_chunks is not None :
10671101 _warn_write_empty_chunks_kwarg ()
10681102
1069- mode = kwargs .pop ("mode" , None )
10701103 if mode is None :
10711104 mode = "a"
10721105 store_path = await make_store_path (store , path = path , mode = mode , storage_options = storage_options )
@@ -1102,7 +1135,7 @@ async def create(
11021135 dimension_names = dimension_names ,
11031136 attributes = attributes ,
11041137 config = config_parsed ,
1105- ** kwargs ,
1138+ data = data ,
11061139 )
11071140
11081141
@@ -1126,14 +1159,19 @@ async def empty(shape: tuple[int, ...], **kwargs: Any) -> AnyAsyncArray:
11261159 return await create (shape = shape , ** kwargs )
11271160
11281161
1129- async def empty_like (a : ArrayLike , ** kwargs : Any ) -> AnyAsyncArray :
1162+ async def empty_like (
1163+ a : ArrayLike , * , zarr_format : ZarrFormat | None = None , ** kwargs : Any
1164+ ) -> AnyAsyncArray :
11301165 """Create an empty array like `a`. The contents will be filled with the
11311166 array's fill value or zeros if no fill value is provided.
11321167
11331168 Parameters
11341169 ----------
11351170 a : array-like
11361171 The array to create an empty array like.
1172+ zarr_format : {2, 3, None}, optional
1173+ The zarr format of the new array. If `None` (default), the zarr format of `a` if it
1174+ is a zarr array, otherwise the default zarr format.
11371175 **kwargs
11381176 Keyword arguments passed to [`create`][zarr.api.asynchronous.create].
11391177
@@ -1148,7 +1186,7 @@ async def empty_like(a: ArrayLike, **kwargs: Any) -> AnyAsyncArray:
11481186 retrieve data from an empty Zarr array, any values may be returned,
11491187 and these are not guaranteed to be stable from one access to the next.
11501188 """
1151- like_kwargs = _like_args (a ) | kwargs
1189+ like_kwargs = _like_args (a , zarr_format ) | kwargs
11521190 return await empty (** like_kwargs ) # type: ignore[arg-type]
11531191
11541192
@@ -1175,13 +1213,18 @@ async def full(shape: tuple[int, ...], fill_value: Any, **kwargs: Any) -> AnyAsy
11751213
11761214
11771215# TODO: add type annotations for kwargs
1178- async def full_like (a : ArrayLike , ** kwargs : Any ) -> AnyAsyncArray :
1216+ async def full_like (
1217+ a : ArrayLike , * , zarr_format : ZarrFormat | None = None , ** kwargs : Any
1218+ ) -> AnyAsyncArray :
11791219 """Create a filled array like `a`.
11801220
11811221 Parameters
11821222 ----------
11831223 a : array-like
11841224 The array to create an empty array like.
1225+ zarr_format : {2, 3, None}, optional
1226+ The zarr format of the new array. If `None` (default), the zarr format of `a` if it
1227+ is a zarr array, otherwise the default zarr format.
11851228 **kwargs
11861229 Keyword arguments passed to [`zarr.api.asynchronous.create`][].
11871230
@@ -1190,7 +1233,7 @@ async def full_like(a: ArrayLike, **kwargs: Any) -> AnyAsyncArray:
11901233 Array
11911234 The new array.
11921235 """
1193- like_kwargs = _like_args (a ) | kwargs
1236+ like_kwargs = _like_args (a , zarr_format ) | kwargs
11941237 return await full (** like_kwargs ) # type: ignore[arg-type]
11951238
11961239
@@ -1213,13 +1256,18 @@ async def ones(shape: tuple[int, ...], **kwargs: Any) -> AnyAsyncArray:
12131256 return await create (shape = shape , fill_value = 1 , ** kwargs )
12141257
12151258
1216- async def ones_like (a : ArrayLike , ** kwargs : Any ) -> AnyAsyncArray :
1259+ async def ones_like (
1260+ a : ArrayLike , * , zarr_format : ZarrFormat | None = None , ** kwargs : Any
1261+ ) -> AnyAsyncArray :
12171262 """Create an array of ones like `a`.
12181263
12191264 Parameters
12201265 ----------
12211266 a : array-like
12221267 The array to create an empty array like.
1268+ zarr_format : {2, 3, None}, optional
1269+ The zarr format of the new array. If `None` (default), the zarr format of `a` if it
1270+ is a zarr array, otherwise the default zarr format.
12231271 **kwargs
12241272 Keyword arguments passed to [`zarr.api.asynchronous.create`][].
12251273
@@ -1228,7 +1276,7 @@ async def ones_like(a: ArrayLike, **kwargs: Any) -> AnyAsyncArray:
12281276 Array
12291277 The new array.
12301278 """
1231- like_args = _like_args (a )
1279+ like_args = _like_args (a , zarr_format )
12321280 # `ones` supplies its own fill_value, so drop any inherited from `a`.
12331281 like_args .pop ("fill_value" , None )
12341282 like_kwargs = like_args | kwargs
@@ -1289,7 +1337,9 @@ async def open_array(
12891337 raise ArrayNotFoundError (msg ) from err
12901338
12911339
1292- async def open_like (a : ArrayLike , path : str , ** kwargs : Any ) -> AnyAsyncArray :
1340+ async def open_like (
1341+ a : ArrayLike , path : str , * , zarr_format : ZarrFormat | None = None , ** kwargs : Any
1342+ ) -> AnyAsyncArray :
12931343 """Open a persistent array like `a`.
12941344
12951345 Parameters
@@ -1298,6 +1348,10 @@ async def open_like(a: ArrayLike, path: str, **kwargs: Any) -> AnyAsyncArray:
12981348 The shape and data-type of a define these same attributes of the returned array.
12991349 path : str
13001350 The path to the new array.
1351+ zarr_format : {2, 3, None}, optional
1352+ The zarr format of the array to open or create. If `None` (default), an existing
1353+ array of either format is opened, and a missing one is created in the default zarr
1354+ format. The zarr format of `a` is not inherited.
13011355 **kwargs
13021356 Additional keyword arguments passed to `open_array`.
13031357 If `mode` is omitted or `None`, it defaults to `"a"`. Pass `mode="r"` when
@@ -1308,10 +1362,14 @@ async def open_like(a: ArrayLike, path: str, **kwargs: Any) -> AnyAsyncArray:
13081362 AsyncArray
13091363 The opened array.
13101364 """
1311- like_kwargs = _like_args (a ) | kwargs
1365+ # The zarr format of `a` is not inherited: `open_array` would then look for an existing
1366+ # array in only that format.
1367+ like_args = _like_args (a , zarr_format or _default_zarr_format ())
1368+ like_args .pop ("zarr_format" )
1369+ like_kwargs = like_args | kwargs
13121370 if like_kwargs .get ("mode" ) is None :
13131371 like_kwargs ["mode" ] = "a"
1314- return await open_array (path = path , ** like_kwargs ) # type: ignore[arg-type]
1372+ return await open_array (path = path , zarr_format = zarr_format , ** like_kwargs ) # type: ignore[arg-type]
13151373
13161374
13171375async def zeros (shape : tuple [int , ...], ** kwargs : Any ) -> AnyAsyncArray :
@@ -1333,13 +1391,18 @@ async def zeros(shape: tuple[int, ...], **kwargs: Any) -> AnyAsyncArray:
13331391 return await create (shape = shape , fill_value = 0 , ** kwargs )
13341392
13351393
1336- async def zeros_like (a : ArrayLike , ** kwargs : Any ) -> AnyAsyncArray :
1394+ async def zeros_like (
1395+ a : ArrayLike , * , zarr_format : ZarrFormat | None = None , ** kwargs : Any
1396+ ) -> AnyAsyncArray :
13371397 """Create an array of zeros like `a`.
13381398
13391399 Parameters
13401400 ----------
13411401 a : array-like
13421402 The array to create an empty array like.
1403+ zarr_format : {2, 3, None}, optional
1404+ The zarr format of the new array. If `None` (default), the zarr format of `a` if it
1405+ is a zarr array, otherwise the default zarr format.
13431406 **kwargs
13441407 Keyword arguments passed to [`create`][zarr.api.asynchronous.create].
13451408
@@ -1348,7 +1411,7 @@ async def zeros_like(a: ArrayLike, **kwargs: Any) -> AnyAsyncArray:
13481411 Array
13491412 The new array.
13501413 """
1351- like_args = _like_args (a )
1414+ like_args = _like_args (a , zarr_format )
13521415 # `zeros` supplies its own fill_value, so drop any inherited from `a`.
13531416 like_args .pop ("fill_value" , None )
13541417 like_kwargs = like_args | kwargs
0 commit comments