@@ -324,19 +324,18 @@ def flatten(
324324 children : dict [str , ArrayV2Metadata | ArrayV3Metadata | GroupMetadata ] = {}
325325 if isinstance (group , ArrayV2Metadata | ArrayV3Metadata ):
326326 children [key ] = group
327+ elif group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
328+ children [key ] = replace (
329+ group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
330+ )
331+ for name , val in group .consolidated_metadata .metadata .items ():
332+ full_key = f"{ key } /{ name } "
333+ if isinstance (val , GroupMetadata ):
334+ children .update (flatten (full_key , val ))
335+ else :
336+ children [full_key ] = val
327337 else :
328- if group .consolidated_metadata and group .consolidated_metadata .metadata is not None :
329- children [key ] = replace (
330- group , consolidated_metadata = ConsolidatedMetadata (metadata = {})
331- )
332- for name , val in group .consolidated_metadata .metadata .items ():
333- full_key = f"{ key } /{ name } "
334- if isinstance (val , GroupMetadata ):
335- children .update (flatten (full_key , val ))
336- else :
337- children [full_key ] = val
338- else :
339- children [key ] = replace (group , consolidated_metadata = None )
338+ children [key ] = replace (group , consolidated_metadata = None )
340339 return children
341340
342341 for k , v in self .metadata .items ():
@@ -1276,9 +1275,8 @@ async def require_array(
12761275 if exact :
12771276 if ds .dtype != dtype :
12781277 raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1279- else :
1280- if not np .can_cast (ds .dtype , dtype ):
1281- raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1278+ elif not np .can_cast (ds .dtype , dtype ):
1279+ raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
12821280 except KeyError :
12831281 ds = await self .create_array (name , shape = shape , dtype = dtype , ** kwargs )
12841282
@@ -3264,24 +3262,23 @@ async def create_hierarchy(
32643262 else :
32653263 # Any other exception is a real error
32663264 raise extant_node
3267- else :
3268- # this is a node that already exists, but a node with the same key was specified
3269- # in nodes_parsed.
3270- if isinstance (extant_node , GroupMetadata ):
3271- # a group already exists where we want to create a group
3272- if isinstance (proposed_node , ImplicitGroupMarker ):
3273- # we have proposed an implicit group, which is OK -- we will just skip
3274- # creating this particular metadata document
3275- redundant_implicit_groups .append (key )
3276- else :
3277- # we have proposed an explicit group, which is an error, given that a
3278- # group already exists.
3279- msg = f"A group exists in store { store !r} at path { key !r} ."
3280- raise ContainsGroupError (msg )
3281- elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3282- # we are trying to overwrite an existing array. this is an error.
3283- msg = f"An array exists in store { store !r} at path { key !r} ."
3284- raise ContainsArrayError (msg )
3265+ # this is a node that already exists, but a node with the same key was specified
3266+ # in nodes_parsed.
3267+ elif isinstance (extant_node , GroupMetadata ):
3268+ # a group already exists where we want to create a group
3269+ if isinstance (proposed_node , ImplicitGroupMarker ):
3270+ # we have proposed an implicit group, which is OK -- we will just skip
3271+ # creating this particular metadata document
3272+ redundant_implicit_groups .append (key )
3273+ else :
3274+ # we have proposed an explicit group, which is an error, given that a
3275+ # group already exists.
3276+ msg = f"A group exists in store { store !r} at path { key !r} ."
3277+ raise ContainsGroupError (msg )
3278+ elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3279+ # we are trying to overwrite an existing array. this is an error.
3280+ msg = f"An array exists in store { store !r} at path { key !r} ."
3281+ raise ContainsArrayError (msg )
32853282
32863283 nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
32873284
@@ -3439,13 +3436,12 @@ def _parse_hierarchy_dict(
34393436 # If a component is not already in the output dict, add ImplicitGroupMetadata
34403437 if subpath not in out :
34413438 out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3442- else :
3443- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3444- msg = (
3445- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3446- "This is invalid. Only Zarr groups can contain other nodes."
3447- )
3448- raise ValueError (msg )
3439+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3440+ msg = (
3441+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3442+ "This is invalid. Only Zarr groups can contain other nodes."
3443+ )
3444+ raise ValueError (msg )
34493445 return out
34503446
34513447
@@ -3649,12 +3645,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
36493645 # return the array metadata.
36503646 if zarray_bytes is not None :
36513647 zmeta = json .loads (zarray_bytes .to_bytes ())
3648+ elif zgroup_bytes is None :
3649+ # neither .zarray or .zgroup were found results in KeyError
3650+ raise FileNotFoundError (path )
36523651 else :
3653- if zgroup_bytes is None :
3654- # neither .zarray or .zgroup were found results in KeyError
3655- raise FileNotFoundError (path )
3656- else :
3657- zmeta = json .loads (zgroup_bytes .to_bytes ())
3652+ zmeta = json .loads (zgroup_bytes .to_bytes ())
36583653
36593654 return _build_metadata_v2 (zmeta , zattrs )
36603655
0 commit comments