@@ -299,19 +299,18 @@ def flatten(
299299 children : dict [str , ArrayV2Metadata | ArrayV3Metadata | GroupMetadata ] = {}
300300 if isinstance (group , ArrayV2Metadata | ArrayV3Metadata ):
301301 children [key ] = group
302+ elif group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
303+ children [key ] = replace (
304+ group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
305+ )
306+ for name , val in group .consolidated_metadata .metadata .items ():
307+ full_key = f"{ key } /{ name } "
308+ if isinstance (val , GroupMetadata ):
309+ children .update (flatten (full_key , val ))
310+ else :
311+ children [full_key ] = val
302312 else :
303- if group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
304- children [key ] = replace (
305- group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
306- )
307- for name , val in group .consolidated_metadata .metadata .items ():
308- full_key = f"{ key } /{ name } "
309- if isinstance (val , GroupMetadata ):
310- children .update (flatten (full_key , val ))
311- else :
312- children [full_key ] = val
313- else :
314- children [key ] = replace (group , consolidated_metadata = None )
313+ children [key ] = replace (group , consolidated_metadata = None )
315314 return children
316315
317316 for k , v in self .metadata .items ():
@@ -1242,9 +1241,8 @@ async def require_array(
12421241 if exact :
12431242 if ds .dtype != dtype :
12441243 raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1245- else :
1246- if not np .can_cast (ds .dtype , dtype ):
1247- raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1244+ elif not np .can_cast (ds .dtype , dtype ):
1245+ raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
12481246 except KeyError :
12491247 ds = await self .create_array (name , shape = shape , dtype = dtype , ** kwargs )
12501248
@@ -3039,22 +3037,21 @@ async def create_hierarchy(
30393037 else :
30403038 # Any other exception is a real error
30413039 raise extant_node
3042- else :
3043- # this is a node that already exists, but a node with the same key was specified
3044- # in nodes_parsed.
3045- if isinstance (extant_node , GroupMetadata ):
3046- # a group already exists where we want to create a group
3047- if isinstance (proposed_node , ImplicitGroupMarker ):
3048- # we have proposed an implicit group, which is OK -- we will just skip
3049- # creating this particular metadata document
3050- redundant_implicit_groups .append (key )
3051- else :
3052- # we have proposed an explicit group, which is an error, given that a
3053- # group already exists.
3054- raise ContainsGroupError (store , key )
3055- elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3056- # we are trying to overwrite an existing array. this is an error.
3057- raise ContainsArrayError (store , key )
3040+ # this is a node that already exists, but a node with the same key was specified
3041+ # in nodes_parsed.
3042+ elif isinstance (extant_node , GroupMetadata ):
3043+ # a group already exists where we want to create a group
3044+ if isinstance (proposed_node , ImplicitGroupMarker ):
3045+ # we have proposed an implicit group, which is OK -- we will just skip
3046+ # creating this particular metadata document
3047+ redundant_implicit_groups .append (key )
3048+ else :
3049+ # we have proposed an explicit group, which is an error, given that a
3050+ # group already exists.
3051+ raise ContainsGroupError (store , key )
3052+ elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3053+ # we are trying to overwrite an existing array. this is an error.
3054+ raise ContainsArrayError (store , key )
30583055
30593056 nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
30603057
@@ -3214,13 +3211,12 @@ def _parse_hierarchy_dict(
32143211 # If a component is not already in the output dict, add ImplicitGroupMetadata
32153212 if subpath not in out :
32163213 out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3217- else :
3218- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3219- msg = (
3220- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3221- "This is invalid. Only Zarr groups can contain other nodes."
3222- )
3223- raise ValueError (msg )
3214+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3215+ msg = (
3216+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3217+ "This is invalid. Only Zarr groups can contain other nodes."
3218+ )
3219+ raise ValueError (msg )
32243220 return out
32253221
32263222
@@ -3412,12 +3408,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
34123408 # return the array metadata.
34133409 if zarray_bytes is not None :
34143410 zmeta = json .loads (zarray_bytes .to_bytes ())
3411+ elif zgroup_bytes is None :
3412+ # neither .zarray or .zgroup were found results in KeyError
3413+ raise FileNotFoundError (path )
34153414 else :
3416- if zgroup_bytes is None :
3417- # neither .zarray or .zgroup were found results in KeyError
3418- raise FileNotFoundError (path )
3419- else :
3420- zmeta = json .loads (zgroup_bytes .to_bytes ())
3415+ zmeta = json .loads (zgroup_bytes .to_bytes ())
34213416
34223417 return _build_metadata_v2 (zmeta , zattrs )
34233418
0 commit comments