12
12
from zarr .core .common import JSON , AccessModeLiteral , ChunkCoords , MemoryOrder , ZarrFormat
13
13
from zarr .core .config import config
14
14
from zarr .core .group import AsyncGroup
15
- from zarr .core .metadata .v2 import ArrayV2Metadata
16
- from zarr .core .metadata .v3 import ArrayV3Metadata
15
+ from zarr .core .metadata import ArrayMetadataDict , ArrayV2Metadata , ArrayV3Metadata
17
16
from zarr .errors import NodeTypeValidationError
18
17
from zarr .storage import (
19
18
StoreLike ,
29
28
from zarr .core .chunk_key_encodings import ChunkKeyEncoding
30
29
31
30
# TODO: this type could use some more thought
32
- ArrayLike = AsyncArray | Array | npt .NDArray [Any ]
31
+ ArrayLike = AsyncArray [ ArrayV2Metadata ] | AsyncArray [ ArrayV3Metadata ] | Array | npt .NDArray [Any ]
33
32
PathLike = str
34
33
35
34
__all__ = [
@@ -98,11 +97,11 @@ def _like_args(a: ArrayLike, kwargs: dict[str, Any]) -> dict[str, Any]:
98
97
if isinstance (a .metadata , ArrayV2Metadata ):
99
98
new ["compressor" ] = a .metadata .compressor
100
99
new ["filters" ] = a .metadata .filters
101
-
102
- if isinstance (a .metadata , ArrayV3Metadata ):
103
- new ["codecs" ] = a .metadata .codecs
104
100
else :
105
- raise TypeError (f"Unsupported zarr format: { a .metadata .zarr_format } " )
101
+ # TODO: Remove type: ignore statement when type inference improves.
102
+ # mypy cannot correctly infer the type of a.metadata here for some reason.
103
+ new ["codecs" ] = a .metadata .codecs # type: ignore[unreachable]
104
+
106
105
else :
107
106
# TODO: set default values compressor/codecs
108
107
# to do this, we may need to evaluate if this is a v2 or v3 array
@@ -199,7 +198,7 @@ async def open(
199
198
path : str | None = None ,
200
199
storage_options : dict [str , Any ] | None = None ,
201
200
** kwargs : Any , # TODO: type kwargs as valid args to open_array
202
- ) -> AsyncArray | AsyncGroup :
201
+ ) -> AsyncArray [ ArrayV2Metadata ] | AsyncArray [ ArrayV3Metadata ] | AsyncGroup :
203
202
"""Convenience function to open a group or array using file-mode-like semantics.
204
203
205
204
Parameters
@@ -237,11 +236,13 @@ async def open(
237
236
if "shape" not in kwargs and mode in {"a" , "w" , "w-" }:
238
237
try :
239
238
metadata_dict = await get_array_metadata (store_path , zarr_format = zarr_format )
239
+ # TODO: remove this cast when we fix typing for array metadata dicts
240
+ _metadata_dict = cast (ArrayMetadataDict , metadata_dict )
240
241
# for v2, the above would already have raised an exception if not an array
241
- zarr_format = metadata_dict ["zarr_format" ]
242
- is_v3_array = zarr_format == 3 and metadata_dict .get ("node_type" ) == "array"
242
+ zarr_format = _metadata_dict ["zarr_format" ]
243
+ is_v3_array = zarr_format == 3 and _metadata_dict .get ("node_type" ) == "array"
243
244
if is_v3_array or zarr_format == 2 :
244
- return AsyncArray (store_path = store_path , metadata = metadata_dict )
245
+ return AsyncArray (store_path = store_path , metadata = _metadata_dict )
245
246
except (AssertionError , FileNotFoundError ):
246
247
pass
247
248
return await open_group (store = store_path , zarr_format = zarr_format , mode = mode , ** kwargs )
@@ -404,7 +405,9 @@ async def tree(*args: Any, **kwargs: Any) -> None:
404
405
raise NotImplementedError
405
406
406
407
407
- async def array (data : npt .ArrayLike , ** kwargs : Any ) -> AsyncArray :
408
+ async def array (
409
+ data : npt .ArrayLike , ** kwargs : Any
410
+ ) -> AsyncArray [ArrayV2Metadata ] | AsyncArray [ArrayV3Metadata ]:
408
411
"""Create an array filled with `data`.
409
412
410
413
Parameters
@@ -658,7 +661,7 @@ async def create(
658
661
dimension_names : Iterable [str ] | None = None ,
659
662
storage_options : dict [str , Any ] | None = None ,
660
663
** kwargs : Any ,
661
- ) -> AsyncArray :
664
+ ) -> AsyncArray [ ArrayV2Metadata ] | AsyncArray [ ArrayV3Metadata ] :
662
665
"""Create an array.
663
666
664
667
Parameters
@@ -810,7 +813,9 @@ async def create(
810
813
)
811
814
812
815
813
- async def empty (shape : ChunkCoords , ** kwargs : Any ) -> AsyncArray :
816
+ async def empty (
817
+ shape : ChunkCoords , ** kwargs : Any
818
+ ) -> AsyncArray [ArrayV2Metadata ] | AsyncArray [ArrayV3Metadata ]:
814
819
"""Create an empty array.
815
820
816
821
Parameters
@@ -829,7 +834,9 @@ async def empty(shape: ChunkCoords, **kwargs: Any) -> AsyncArray:
829
834
return await create (shape = shape , fill_value = None , ** kwargs )
830
835
831
836
832
- async def empty_like (a : ArrayLike , ** kwargs : Any ) -> AsyncArray :
837
+ async def empty_like (
838
+ a : ArrayLike , ** kwargs : Any
839
+ ) -> AsyncArray [ArrayV2Metadata ] | AsyncArray [ArrayV3Metadata ]:
833
840
"""Create an empty array like `a`.
834
841
835
842
Parameters
@@ -849,7 +856,9 @@ async def empty_like(a: ArrayLike, **kwargs: Any) -> AsyncArray:
849
856
850
857
851
858
# TODO: add type annotations for fill_value and kwargs
852
- async def full (shape : ChunkCoords , fill_value : Any , ** kwargs : Any ) -> AsyncArray :
859
+ async def full (
860
+ shape : ChunkCoords , fill_value : Any , ** kwargs : Any
861
+ ) -> AsyncArray [ArrayV2Metadata ] | AsyncArray [ArrayV3Metadata ]:
853
862
"""Create an array, with `fill_value` being used as the default value for
854
863
uninitialized portions of the array.
855
864
@@ -871,7 +880,9 @@ async def full(shape: ChunkCoords, fill_value: Any, **kwargs: Any) -> AsyncArray
871
880
872
881
873
882
# TODO: add type annotations for kwargs
874
- async def full_like (a : ArrayLike , ** kwargs : Any ) -> AsyncArray :
883
+ async def full_like (
884
+ a : ArrayLike , ** kwargs : Any
885
+ ) -> AsyncArray [ArrayV2Metadata ] | AsyncArray [ArrayV3Metadata ]:
875
886
"""Create a filled array like `a`.
876
887
877
888
Parameters
@@ -892,7 +903,9 @@ async def full_like(a: ArrayLike, **kwargs: Any) -> AsyncArray:
892
903
return await full (** like_kwargs )
893
904
894
905
895
- async def ones (shape : ChunkCoords , ** kwargs : Any ) -> AsyncArray :
906
+ async def ones (
907
+ shape : ChunkCoords , ** kwargs : Any
908
+ ) -> AsyncArray [ArrayV2Metadata ] | AsyncArray [ArrayV3Metadata ]:
896
909
"""Create an array, with one being used as the default value for
897
910
uninitialized portions of the array.
898
911
@@ -911,7 +924,9 @@ async def ones(shape: ChunkCoords, **kwargs: Any) -> AsyncArray:
911
924
return await create (shape = shape , fill_value = 1 , ** kwargs )
912
925
913
926
914
- async def ones_like (a : ArrayLike , ** kwargs : Any ) -> AsyncArray :
927
+ async def ones_like (
928
+ a : ArrayLike , ** kwargs : Any
929
+ ) -> AsyncArray [ArrayV2Metadata ] | AsyncArray [ArrayV3Metadata ]:
915
930
"""Create an array of ones like `a`.
916
931
917
932
Parameters
@@ -938,7 +953,7 @@ async def open_array(
938
953
path : PathLike | None = None ,
939
954
storage_options : dict [str , Any ] | None = None ,
940
955
** kwargs : Any , # TODO: type kwargs as valid args to save
941
- ) -> AsyncArray :
956
+ ) -> AsyncArray [ ArrayV2Metadata ] | AsyncArray [ ArrayV3Metadata ] :
942
957
"""Open an array using file-mode-like semantics.
943
958
944
959
Parameters
@@ -981,7 +996,9 @@ async def open_array(
981
996
raise
982
997
983
998
984
- async def open_like (a : ArrayLike , path : str , ** kwargs : Any ) -> AsyncArray :
999
+ async def open_like (
1000
+ a : ArrayLike , path : str , ** kwargs : Any
1001
+ ) -> AsyncArray [ArrayV3Metadata ] | AsyncArray [ArrayV2Metadata ]:
985
1002
"""Open a persistent array like `a`.
986
1003
987
1004
Parameters
@@ -1004,7 +1021,9 @@ async def open_like(a: ArrayLike, path: str, **kwargs: Any) -> AsyncArray:
1004
1021
return await open_array (path = path , ** like_kwargs )
1005
1022
1006
1023
1007
- async def zeros (shape : ChunkCoords , ** kwargs : Any ) -> AsyncArray :
1024
+ async def zeros (
1025
+ shape : ChunkCoords , ** kwargs : Any
1026
+ ) -> AsyncArray [ArrayV2Metadata ] | AsyncArray [ArrayV3Metadata ]:
1008
1027
"""Create an array, with zero being used as the default value for
1009
1028
uninitialized portions of the array.
1010
1029
@@ -1023,7 +1042,9 @@ async def zeros(shape: ChunkCoords, **kwargs: Any) -> AsyncArray:
1023
1042
return await create (shape = shape , fill_value = 0 , ** kwargs )
1024
1043
1025
1044
1026
- async def zeros_like (a : ArrayLike , ** kwargs : Any ) -> AsyncArray :
1045
+ async def zeros_like (
1046
+ a : ArrayLike , ** kwargs : Any
1047
+ ) -> AsyncArray [ArrayV2Metadata ] | AsyncArray [ArrayV3Metadata ]:
1027
1048
"""Create an array of zeros like `a`.
1028
1049
1029
1050
Parameters
0 commit comments