@@ -299,19 +299,18 @@ def flatten(
299
299
children : dict [str , ArrayV2Metadata | ArrayV3Metadata | GroupMetadata ] = {}
300
300
if isinstance (group , ArrayV2Metadata | ArrayV3Metadata ):
301
301
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
302
312
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 )
315
314
return children
316
315
317
316
for k , v in self .metadata .items ():
@@ -1256,9 +1255,8 @@ async def require_array(
1256
1255
if exact :
1257
1256
if ds .dtype != dtype :
1258
1257
raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1259
- else :
1260
- if not np .can_cast (ds .dtype , dtype ):
1261
- raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1258
+ elif not np .can_cast (ds .dtype , dtype ):
1259
+ raise TypeError (f"Incompatible dtype ({ ds .dtype } vs { dtype } )" )
1262
1260
except KeyError :
1263
1261
ds = await self .create_array (name , shape = shape , dtype = dtype , ** kwargs )
1264
1262
@@ -3092,22 +3090,21 @@ async def create_hierarchy(
3092
3090
else :
3093
3091
# Any other exception is a real error
3094
3092
raise extant_node
3095
- else :
3096
- # this is a node that already exists, but a node with the same key was specified
3097
- # in nodes_parsed.
3098
- if isinstance (extant_node , GroupMetadata ):
3099
- # a group already exists where we want to create a group
3100
- if isinstance (proposed_node , ImplicitGroupMarker ):
3101
- # we have proposed an implicit group, which is OK -- we will just skip
3102
- # creating this particular metadata document
3103
- redundant_implicit_groups .append (key )
3104
- else :
3105
- # we have proposed an explicit group, which is an error, given that a
3106
- # group already exists.
3107
- raise ContainsGroupError (store , key )
3108
- elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3109
- # we are trying to overwrite an existing array. this is an error.
3110
- raise ContainsArrayError (store , key )
3093
+ # this is a node that already exists, but a node with the same key was specified
3094
+ # in nodes_parsed.
3095
+ elif isinstance (extant_node , GroupMetadata ):
3096
+ # a group already exists where we want to create a group
3097
+ if isinstance (proposed_node , ImplicitGroupMarker ):
3098
+ # we have proposed an implicit group, which is OK -- we will just skip
3099
+ # creating this particular metadata document
3100
+ redundant_implicit_groups .append (key )
3101
+ else :
3102
+ # we have proposed an explicit group, which is an error, given that a
3103
+ # group already exists.
3104
+ raise ContainsGroupError (store , key )
3105
+ elif isinstance (extant_node , ArrayV2Metadata | ArrayV3Metadata ):
3106
+ # we are trying to overwrite an existing array. this is an error.
3107
+ raise ContainsArrayError (store , key )
3111
3108
3112
3109
nodes_explicit : dict [str , GroupMetadata | ArrayV2Metadata | ArrayV3Metadata ] = {}
3113
3110
@@ -3267,13 +3264,12 @@ def _parse_hierarchy_dict(
3267
3264
# If a component is not already in the output dict, add ImplicitGroupMetadata
3268
3265
if subpath not in out :
3269
3266
out [subpath ] = ImplicitGroupMarker (zarr_format = v .zarr_format )
3270
- else :
3271
- if not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3272
- msg = (
3273
- f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3274
- "This is invalid. Only Zarr groups can contain other nodes."
3275
- )
3276
- raise ValueError (msg )
3267
+ elif not isinstance (out [subpath ], GroupMetadata | ImplicitGroupMarker ):
3268
+ msg = (
3269
+ f"The node at { subpath } contains other nodes, but it is not a Zarr group. "
3270
+ "This is invalid. Only Zarr groups can contain other nodes."
3271
+ )
3272
+ raise ValueError (msg )
3277
3273
return out
3278
3274
3279
3275
@@ -3481,12 +3477,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
3481
3477
# return the array metadata.
3482
3478
if zarray_bytes is not None :
3483
3479
zmeta = json .loads (zarray_bytes .to_bytes ())
3480
+ elif zgroup_bytes is None :
3481
+ # neither .zarray or .zgroup were found results in KeyError
3482
+ raise FileNotFoundError (path )
3484
3483
else :
3485
- if zgroup_bytes is None :
3486
- # neither .zarray or .zgroup were found results in KeyError
3487
- raise FileNotFoundError (path )
3488
- else :
3489
- zmeta = json .loads (zgroup_bytes .to_bytes ())
3484
+ zmeta = json .loads (zgroup_bytes .to_bytes ())
3490
3485
3491
3486
return _build_metadata_v2 (zmeta , zattrs )
3492
3487
0 commit comments