@@ -300,19 +300,18 @@ def flatten(
300300 children : dict [str , ArrayV2Metadata | ArrayV3Metadata | GroupMetadata ] = {}
301301 if isinstance (group , ArrayV2Metadata | ArrayV3Metadata ):
302302 children [key ] = group
303+ elif 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
303313 else :
304- if group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
305- children [key ] = replace (
306- group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
307- )
308- for name , val in group .consolidated_metadata .metadata .items ():
309- full_key = f"{ key } /{ name } "
310- if isinstance (val , GroupMetadata ):
311- children .update (flatten (full_key , val ))
312- else :
313- children [full_key ] = val
314- else :
315- children [key ] = replace (group , consolidated_metadata = None )
314+ children [key ] = replace (group , consolidated_metadata = None )
316315 return children
317316
318317 for k , v in self .metadata .items ():
@@ -1243,9 +1242,8 @@ async def require_array(
12431242 if exact :
12441243 if ds .dtype != dtype :
12451244 raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1246- else :
1247- if not np .can_cast (ds .dtype , dtype ):
1248- raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1245+ elif not np .can_cast (ds .dtype , dtype ):
1246+ raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
12491247 except KeyError :
12501248 ds = await self .create_array (name , shape = shape , dtype = dtype , ** kwargs )
12511249
@@ -3040,22 +3038,21 @@ async def create_hierarchy(
30403038 else :
30413039 # Any other exception is a real error
30423040 raise extant_node
3043- else :
3044- # this is a node that already exists, but a node with the same key was specified
3045- # in nodes_parsed.
3046- if isinstance (extant_node , GroupMetadata ):
3047- # a group already exists where we want to create a group
3048- if isinstance (proposed_node , ImplicitGroupMarker ):
3049- # we have proposed an implicit group, which is OK -- we will just skip
3050- # creating this particular metadata document
3051- redundant_implicit_groups .append (key )
3052- else :
3053- # we have proposed an explicit group, which is an error, given that a
3054- # group already exists.
3055- raise ContainsGroupError (store , key )
3056- elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3057- # we are trying to overwrite an existing array. this is an error.
3058- raise ContainsArrayError (store , key )
3041+ # this is a node that already exists, but a node with the same key was specified
3042+ # in nodes_parsed.
3043+ elif isinstance (extant_node , GroupMetadata ):
3044+ # a group already exists where we want to create a group
3045+ if isinstance (proposed_node , ImplicitGroupMarker ):
3046+ # we have proposed an implicit group, which is OK -- we will just skip
3047+ # creating this particular metadata document
3048+ redundant_implicit_groups .append (key )
3049+ else :
3050+ # we have proposed an explicit group, which is an error, given that a
3051+ # group already exists.
3052+ raise ContainsGroupError (store , key )
3053+ elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3054+ # we are trying to overwrite an existing array. this is an error.
3055+ raise ContainsArrayError (store , key )
30593056
30603057 nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
30613058
@@ -3215,13 +3212,12 @@ def _parse_hierarchy_dict(
32153212 # If a component is not already in the output dict, add ImplicitGroupMetadata
32163213 if subpath not in out :
32173214 out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3218- else :
3219- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3220- msg = (
3221- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3222- "This is invalid. Only Zarr groups can contain other nodes."
3223- )
3224- raise ValueError (msg )
3215+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3216+ msg = (
3217+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3218+ "This is invalid. Only Zarr groups can contain other nodes."
3219+ )
3220+ raise ValueError (msg )
32253221 return out
32263222
32273223
@@ -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