@@ -315,19 +315,18 @@ def flatten(
315315 children : dict [str , ArrayV2Metadata | ArrayV3Metadata | GroupMetadata ] = {}
316316 if isinstance (group , ArrayV2Metadata | ArrayV3Metadata ):
317317 children [key ] = group
318+ elif group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
319+ children [key ] = replace (
320+ group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
321+ )
322+ for name , val in group .consolidated_metadata .metadata .items ():
323+ full_key = f"{ key } /{ name } "
324+ if isinstance (val , GroupMetadata ):
325+ children .update (flatten (full_key , val ))
326+ else :
327+ children [full_key ] = val
318328 else :
319- if group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
320- children [key ] = replace (
321- group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
322- )
323- for name , val in group .consolidated_metadata .metadata .items ():
324- full_key = f"{ key } /{ name } "
325- if isinstance (val , GroupMetadata ):
326- children .update (flatten (full_key , val ))
327- else :
328- children [full_key ] = val
329- else :
330- children [key ] = replace (group , consolidated_metadata = None )
329+ children [key ] = replace (group , consolidated_metadata = None )
331330 return children
332331
333332 for k , v in self .metadata .items ():
@@ -1273,9 +1272,8 @@ async def require_array(
12731272 if exact :
12741273 if ds .dtype != dtype :
12751274 raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1276- else :
1277- if not np .can_cast (ds .dtype , dtype ):
1278- raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1275+ elif not np .can_cast (ds .dtype , dtype ):
1276+ raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
12791277 except KeyError :
12801278 ds = await self .create_array (name , shape = shape , dtype = dtype , ** kwargs )
12811279
@@ -3119,24 +3117,23 @@ async def create_hierarchy(
31193117 else :
31203118 # Any other exception is a real error
31213119 raise extant_node
3122- else :
3123- # this is a node that already exists, but a node with the same key was specified
3124- # in nodes_parsed.
3125- if isinstance (extant_node , GroupMetadata ):
3126- # a group already exists where we want to create a group
3127- if isinstance (proposed_node , ImplicitGroupMarker ):
3128- # we have proposed an implicit group, which is OK -- we will just skip
3129- # creating this particular metadata document
3130- redundant_implicit_groups .append (key )
3131- else :
3132- # we have proposed an explicit group, which is an error, given that a
3133- # group already exists.
3134- msg = f"A group exists in store { store !r} at path { key !r} ."
3135- raise ContainsGroupError (msg )
3136- elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3137- # we are trying to overwrite an existing array. this is an error.
3138- msg = f"An array exists in store { store !r} at path { key !r} ."
3139- raise ContainsArrayError (msg )
3120+ # this is a node that already exists, but a node with the same key was specified
3121+ # in nodes_parsed.
3122+ elif isinstance (extant_node , GroupMetadata ):
3123+ # a group already exists where we want to create a group
3124+ if isinstance (proposed_node , ImplicitGroupMarker ):
3125+ # we have proposed an implicit group, which is OK -- we will just skip
3126+ # creating this particular metadata document
3127+ redundant_implicit_groups .append (key )
3128+ else :
3129+ # we have proposed an explicit group, which is an error, given that a
3130+ # group already exists.
3131+ msg = f"A group exists in store { store !r} at path { key !r} ."
3132+ raise ContainsGroupError (msg )
3133+ elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3134+ # we are trying to overwrite an existing array. this is an error.
3135+ msg = f"An array exists in store { store !r} at path { key !r} ."
3136+ raise ContainsArrayError (msg )
31403137
31413138 nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
31423139
@@ -3296,13 +3293,12 @@ def _parse_hierarchy_dict(
32963293 # If a component is not already in the output dict, add ImplicitGroupMetadata
32973294 if subpath not in out :
32983295 out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3299- else :
3300- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3301- msg = (
3302- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3303- "This is invalid. Only Zarr groups can contain other nodes."
3304- )
3305- raise ValueError (msg )
3296+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3297+ msg = (
3298+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3299+ "This is invalid. Only Zarr groups can contain other nodes."
3300+ )
3301+ raise ValueError (msg )
33063302 return out
33073303
33083304
@@ -3510,12 +3506,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
35103506 # return the array metadata.
35113507 if zarray_bytes is not None :
35123508 zmeta = json .loads (zarray_bytes .to_bytes ())
3509+ elif zgroup_bytes is None :
3510+ # neither .zarray or .zgroup were found results in KeyError
3511+ raise FileNotFoundError (path )
35133512 else :
3514- if zgroup_bytes is None :
3515- # neither .zarray or .zgroup were found results in KeyError
3516- raise FileNotFoundError (path )
3517- else :
3518- zmeta = json .loads (zgroup_bytes .to_bytes ())
3513+ zmeta = json .loads (zgroup_bytes .to_bytes ())
35193514
35203515 return _build_metadata_v2 (zmeta , zattrs )
35213516
0 commit comments