@@ -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 ():
@@ -1272,9 +1271,8 @@ async def require_array(
12721271 if exact :
12731272 if ds .dtype != dtype :
12741273 raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1275- else :
1276- if not np .can_cast (ds .dtype , dtype ):
1277- raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1274+ elif not np .can_cast (ds .dtype , dtype ):
1275+ raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
12781276 except KeyError :
12791277 ds = await self .create_array (name , shape = shape , dtype = dtype , ** kwargs )
12801278
@@ -3119,22 +3117,21 @@ 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- raise ContainsGroupError (store , key )
3135- elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3136- # we are trying to overwrite an existing array. this is an error.
3137- raise ContainsArrayError (store , key )
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+ raise ContainsGroupError (store , key )
3132+ elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3133+ # we are trying to overwrite an existing array. this is an error.
3134+ raise ContainsArrayError (store , key )
31383135
31393136 nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
31403137
@@ -3294,13 +3291,12 @@ def _parse_hierarchy_dict(
32943291 # If a component is not already in the output dict, add ImplicitGroupMetadata
32953292 if subpath not in out :
32963293 out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3297- else :
3298- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3299- msg = (
3300- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3301- "This is invalid. Only Zarr groups can contain other nodes."
3302- )
3303- raise ValueError (msg )
3294+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3295+ msg = (
3296+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3297+ "This is invalid. Only Zarr groups can contain other nodes."
3298+ )
3299+ raise ValueError (msg )
33043300 return out
33053301
33063302
@@ -3508,12 +3504,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
35083504 # return the array metadata.
35093505 if zarray_bytes is not None :
35103506 zmeta = json .loads (zarray_bytes .to_bytes ())
3507+ elif zgroup_bytes is None :
3508+ # neither .zarray or .zgroup were found results in KeyError
3509+ raise FileNotFoundError (path )
35113510 else :
3512- if zgroup_bytes is None :
3513- # neither .zarray or .zgroup were found results in KeyError
3514- raise FileNotFoundError (path )
3515- else :
3516- zmeta = json .loads (zgroup_bytes .to_bytes ())
3511+ zmeta = json .loads (zgroup_bytes .to_bytes ())
35173512
35183513 return _build_metadata_v2 (zmeta , zattrs )
35193514
0 commit comments