Skip to content

Commit cb6d90a

Browse files
Apply ruff/Pylint rule PLR5501
PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
1 parent af07276 commit cb6d90a

File tree

2 files changed

+43
-49
lines changed

2 files changed

+43
-49
lines changed

src/zarr/core/codec_pipeline.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,12 @@ async def _read_key(
405405
):
406406
if chunk_array is None:
407407
chunk_array_batch.append(None) # type: ignore[unreachable]
408+
elif not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
409+
fill_value_or_default(chunk_spec)
410+
):
411+
chunk_array_batch.append(None)
408412
else:
409-
if not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
410-
fill_value_or_default(chunk_spec)
411-
):
412-
chunk_array_batch.append(None)
413-
else:
414-
chunk_array_batch.append(chunk_array)
413+
chunk_array_batch.append(chunk_array)
415414

416415
chunk_bytes_batch = await self.encode_batch(
417416
[

src/zarr/core/group.py

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,18 @@ def flatten(
299299
children: dict[str, ArrayV2Metadata | ArrayV3Metadata | GroupMetadata] = {}
300300
if isinstance(group, ArrayV2Metadata | ArrayV3Metadata):
301301
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
302312
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)
315314
return children
316315

317316
for k, v in self.metadata.items():
@@ -1256,9 +1255,8 @@ async def require_array(
12561255
if exact:
12571256
if ds.dtype != dtype:
12581257
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})")
12621260
except KeyError:
12631261
ds = await self.create_array(name, shape=shape, dtype=dtype, **kwargs)
12641262

@@ -3092,22 +3090,21 @@ async def create_hierarchy(
30923090
else:
30933091
# Any other exception is a real error
30943092
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)
31113108

31123109
nodes_explicit: dict[str, GroupMetadata | ArrayV2Metadata | ArrayV3Metadata] = {}
31133110

@@ -3267,13 +3264,12 @@ def _parse_hierarchy_dict(
32673264
# If a component is not already in the output dict, add ImplicitGroupMetadata
32683265
if subpath not in out:
32693266
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)
32773273
return out
32783274

32793275

@@ -3481,12 +3477,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
34813477
# return the array metadata.
34823478
if zarray_bytes is not None:
34833479
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)
34843483
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())
34903485

34913486
return _build_metadata_v2(zmeta, zattrs)
34923487

0 commit comments

Comments
 (0)