@@ -88,31 +88,94 @@ def check_array_path(
8888 If the array doesn't exist, or the array is not the expected Zarr version.
8989 """
9090 try :
91- array = zarr .open_array (store = group .store , path = array_path , mode = "r" )
92- array_spec : AnyArraySpecv2 | AnyArraySpecv3
93- if array .metadata .zarr_format == 2 :
94- if expected_zarr_version == 3 :
95- raise ValueError ("Expected Zarr v3 array, but got v2 array" )
96- array_spec = ArraySpecv2 .from_zarr (array )
97- else :
98- if expected_zarr_version == 2 :
99- raise ValueError ("Expected Zarr v2 array, but got v3 array" )
100- array_spec = ArraySpecv3 .from_zarr (array )
91+ array = zarr .open_array (store = group .store_path , path = array_path , mode = "r" )
10192 except FileNotFoundError as e :
10293 msg = (
10394 f"Expected to find an array at { array_path } , but no array was found there."
10495 )
10596 raise ValueError (msg ) from e
106- except zarr .errors .ContainsGroupError as e :
97+ except ( zarr .errors .ContainsGroupError , zarr . errors . NodeTypeValidationError ) as e :
10798 msg = (
10899 f"Expected to find an array at { array_path } , "
109100 "but a group was found there instead."
110101 )
111102 raise ValueError (msg ) from e
112103
104+ array_spec : AnyArraySpecv2 | AnyArraySpecv3
105+ if array .metadata .zarr_format == 2 :
106+ if expected_zarr_version == 3 :
107+ raise ValueError ("Expected Zarr v3 array, but got v2 array" )
108+ array_spec = ArraySpecv2 .from_zarr (array )
109+ else :
110+ if expected_zarr_version == 2 :
111+ raise ValueError ("Expected Zarr v2 array, but got v3 array" )
112+ array_spec = ArraySpecv3 .from_zarr (array )
113+
113114 return array_spec
114115
115116
117+ @overload
118+ def check_group_path (
119+ group : zarr .Group ,
120+ group_path : str ,
121+ * ,
122+ expected_zarr_version : Literal [2 ],
123+ ) -> AnyGroupSpecv2 : ...
124+
125+
126+ @overload
127+ def check_group_path (
128+ group : zarr .Group ,
129+ group_path : str ,
130+ * ,
131+ expected_zarr_version : Literal [3 ],
132+ ) -> AnyGroupSpecv3 : ...
133+
134+
135+ def check_group_path (
136+ group : zarr .Group ,
137+ group_path : str ,
138+ * ,
139+ expected_zarr_version : Literal [2 , 3 ],
140+ ) -> AnyGroupSpecv2 | AnyGroupSpecv3 :
141+ """
142+ Check if a group exists at a given path in a group.
143+
144+ Returns
145+ -------
146+ GroupSpec
147+ If the path exists, it's GroupSpec is returned.
148+
149+ Raises
150+ ------
151+ ValueError
152+ If the group doesn't exist, or the group is not the expected Zarr version.
153+ """
154+ try :
155+ group = zarr .open_group (store = group .store_path , path = group_path , mode = "r" )
156+ except FileNotFoundError as e :
157+ msg = f"Expected to find a group at { group_path } , but no group was found there."
158+ raise FileNotFoundError (msg ) from e
159+ except zarr .errors .ContainsArrayError as e :
160+ msg = (
161+ f"Expected to find an group at { group_path } , "
162+ "but an array was found there instead."
163+ )
164+ raise zarr .errors .ContainsArrayError (msg ) from e
165+
166+ group_spec : AnyGroupSpecv2 | AnyGroupSpecv3
167+ if group .metadata .zarr_format == 2 :
168+ if expected_zarr_version == 3 :
169+ raise ValueError ("Expected Zarr v3 array, but got v2 array" )
170+ group_spec = GroupSpecv2 .from_zarr (group , depth = 0 )
171+ else :
172+ if expected_zarr_version == 2 :
173+ raise ValueError ("Expected Zarr v2 array, but got v3 array" )
174+ group_spec = GroupSpecv3 .from_zarr (group , depth = 0 )
175+
176+ return group_spec
177+
178+
116179def check_length (
117180 sequence : Sequence [T ], * , valid_lengths : Sequence [int ], variable_name : str
118181) -> None :
0 commit comments