33import asyncio
44import dataclasses
55import warnings
6- from typing import TYPE_CHECKING , Any , Literal , cast
6+ from typing import TYPE_CHECKING , Any , Literal , NotRequired , TypedDict , cast
77
88import numpy as np
99import numpy .typing as npt
5656 from zarr .abc .numcodec import Numcodec
5757 from zarr .core .buffer import NDArrayLikeOrScalar
5858 from zarr .core .chunk_key_encodings import ChunkKeyEncoding
59+ from zarr .core .metadata .v2 import CompressorLikev2
5960 from zarr .storage import StoreLike
6061
6162 # TODO: this type could use some more thought
@@ -124,10 +125,20 @@ def _get_shape_chunks(a: ArrayLike | Any) -> tuple[tuple[int, ...] | None, tuple
124125 return shape , chunks
125126
126127
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 :
128139 """Set default values for shape and chunks if they are not present in the array-like object"""
129140
130- new = kwargs . copy ()
141+ new : _LikeArgs = {}
131142
132143 shape , chunks = _get_shape_chunks (a )
133144 if shape is not None :
@@ -138,9 +149,9 @@ def _like_args(a: ArrayLike, kwargs: dict[str, Any]) -> dict[str, Any]:
138149 if hasattr (a , "dtype" ):
139150 new ["dtype" ] = a .dtype
140151
141- if isinstance (a , AsyncArray ):
142- new ["order" ] = a .order
152+ if isinstance (a , AsyncArray | Array ):
143153 if isinstance (a .metadata , ArrayV2Metadata ):
154+ new ["order" ] = a .order
144155 new ["compressor" ] = a .metadata .compressor
145156 new ["filters" ] = a .metadata .filters
146157 else :
@@ -1087,7 +1098,7 @@ async def empty(
10871098 shape : tuple [int , ...], ** kwargs : Any
10881099) -> AsyncArray [ArrayV2Metadata ] | AsyncArray [ArrayV3Metadata ]:
10891100 """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.
10911102
10921103 Parameters
10931104 ----------
@@ -1102,8 +1113,7 @@ async def empty(
11021113 retrieve data from an empty Zarr array, any values may be returned,
11031114 and these are not guaranteed to be stable from one access to the next.
11041115 """
1105-
1106- return await create (shape = shape , fill_value = None , ** kwargs )
1116+ return await create (shape = shape , ** kwargs )
11071117
11081118
11091119async def empty_like (
@@ -1130,8 +1140,10 @@ async def empty_like(
11301140 retrieve data from an empty Zarr array, any values may be returned,
11311141 and these are not guaranteed to be stable from one access to the next.
11321142 """
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]
11351147
11361148
11371149# TODO: add type annotations for fill_value and kwargs
@@ -1176,10 +1188,10 @@ async def full_like(
11761188 Array
11771189 The new array.
11781190 """
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 ) ):
11811193 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]
11831195
11841196
11851197async def ones (
@@ -1220,8 +1232,8 @@ async def ones_like(
12201232 Array
12211233 The new array.
12221234 """
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]
12251237
12261238
12271239async def open_array (
@@ -1300,10 +1312,10 @@ async def open_like(
13001312 AsyncArray
13011313 The opened array.
13021314 """
1303- like_kwargs = _like_args (a , kwargs )
1315+ like_kwargs = _like_args (a ) | kwargs
13041316 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]
13071319
13081320
13091321async def zeros (
@@ -1344,5 +1356,5 @@ async def zeros_like(
13441356 Array
13451357 The new array.
13461358 """
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