Skip to content

Commit 1fc75ab

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

File tree

3 files changed

+60
-71
lines changed

3 files changed

+60
-71
lines changed

src/zarr/core/codec_pipeline.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,12 @@ async def _read_key(
410410
):
411411
if chunk_array is None:
412412
chunk_array_batch.append(None) # type: ignore[unreachable]
413+
elif not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
414+
fill_value_or_default(chunk_spec)
415+
):
416+
chunk_array_batch.append(None)
413417
else:
414-
if not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
415-
fill_value_or_default(chunk_spec)
416-
):
417-
chunk_array_batch.append(None)
418-
else:
419-
chunk_array_batch.append(chunk_array)
418+
chunk_array_batch.append(chunk_array)
420419

421420
chunk_bytes_batch = await self.encode_batch(
422421
[

src/zarr/core/group.py

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,18 @@ def flatten(
319319
children: dict[str, ArrayV2Metadata | ArrayV3Metadata | GroupMetadata] = {}
320320
if isinstance(group, ArrayV2Metadata | ArrayV3Metadata):
321321
children[key] = group
322+
elif group.consolidated_metadata and group.consolidated_metadata.metadata is not None:
323+
children[key] = replace(
324+
group, consolidated_metadata=ConsolidatedMetadata(metadata={})
325+
)
326+
for name, val in group.consolidated_metadata.metadata.items():
327+
full_key = f"{key}/{name}"
328+
if isinstance(val, GroupMetadata):
329+
children.update(flatten(full_key, val))
330+
else:
331+
children[full_key] = val
322332
else:
323-
if group.consolidated_metadata and group.consolidated_metadata.metadata is not None:
324-
children[key] = replace(
325-
group, consolidated_metadata=ConsolidatedMetadata(metadata={})
326-
)
327-
for name, val in group.consolidated_metadata.metadata.items():
328-
full_key = f"{key}/{name}"
329-
if isinstance(val, GroupMetadata):
330-
children.update(flatten(full_key, val))
331-
else:
332-
children[full_key] = val
333-
else:
334-
children[key] = replace(group, consolidated_metadata=None)
333+
children[key] = replace(group, consolidated_metadata=None)
335334
return children
336335

337336
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

@@ -3274,24 +3272,23 @@ async def create_hierarchy(
32743272
else:
32753273
# Any other exception is a real error
32763274
raise extant_node
3277-
else:
3278-
# this is a node that already exists, but a node with the same key was specified
3279-
# in nodes_parsed.
3280-
if isinstance(extant_node, GroupMetadata):
3281-
# a group already exists where we want to create a group
3282-
if isinstance(proposed_node, ImplicitGroupMarker):
3283-
# we have proposed an implicit group, which is OK -- we will just skip
3284-
# creating this particular metadata document
3285-
redundant_implicit_groups.append(key)
3286-
else:
3287-
# we have proposed an explicit group, which is an error, given that a
3288-
# group already exists.
3289-
msg = f"A group exists in store {store!r} at path {key!r}."
3290-
raise ContainsGroupError(msg)
3291-
elif isinstance(extant_node, ArrayV2Metadata | ArrayV3Metadata):
3292-
# we are trying to overwrite an existing array. this is an error.
3293-
msg = f"An array exists in store {store!r} at path {key!r}."
3294-
raise ContainsArrayError(msg)
3275+
# this is a node that already exists, but a node with the same key was specified
3276+
# in nodes_parsed.
3277+
elif isinstance(extant_node, GroupMetadata):
3278+
# a group already exists where we want to create a group
3279+
if isinstance(proposed_node, ImplicitGroupMarker):
3280+
# we have proposed an implicit group, which is OK -- we will just skip
3281+
# creating this particular metadata document
3282+
redundant_implicit_groups.append(key)
3283+
else:
3284+
# we have proposed an explicit group, which is an error, given that a
3285+
# group already exists.
3286+
msg = f"A group exists in store {store!r} at path {key!r}."
3287+
raise ContainsGroupError(msg)
3288+
elif isinstance(extant_node, ArrayV2Metadata | ArrayV3Metadata):
3289+
# we are trying to overwrite an existing array. this is an error.
3290+
msg = f"An array exists in store {store!r} at path {key!r}."
3291+
raise ContainsArrayError(msg)
32953292

32963293
nodes_explicit: dict[str, GroupMetadata | ArrayV2Metadata | ArrayV3Metadata] = {}
32973294

@@ -3451,13 +3448,12 @@ def _parse_hierarchy_dict(
34513448
# If a component is not already in the output dict, add ImplicitGroupMetadata
34523449
if subpath not in out:
34533450
out[subpath] = ImplicitGroupMarker(zarr_format=v.zarr_format)
3454-
else:
3455-
if not isinstance(out[subpath], GroupMetadata | ImplicitGroupMarker):
3456-
msg = (
3457-
f"The node at {subpath} contains other nodes, but it is not a Zarr group. "
3458-
"This is invalid. Only Zarr groups can contain other nodes."
3459-
)
3460-
raise ValueError(msg)
3451+
elif not isinstance(out[subpath], GroupMetadata | ImplicitGroupMarker):
3452+
msg = (
3453+
f"The node at {subpath} contains other nodes, but it is not a Zarr group. "
3454+
"This is invalid. Only Zarr groups can contain other nodes."
3455+
)
3456+
raise ValueError(msg)
34613457
return out
34623458

34633459

@@ -3665,12 +3661,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
36653661
# return the array metadata.
36663662
if zarray_bytes is not None:
36673663
zmeta = json.loads(zarray_bytes.to_bytes())
3664+
elif zgroup_bytes is None:
3665+
# neither .zarray or .zgroup were found results in KeyError
3666+
raise FileNotFoundError(path)
36683667
else:
3669-
if zgroup_bytes is None:
3670-
# neither .zarray or .zgroup were found results in KeyError
3671-
raise FileNotFoundError(path)
3672-
else:
3673-
zmeta = json.loads(zgroup_bytes.to_bytes())
3668+
zmeta = json.loads(zgroup_bytes.to_bytes())
36743669

36753670
return _build_metadata_v2(zmeta, zattrs)
36763671

tests/test_group.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -372,16 +372,13 @@ def test_group_getitem(store: Store, zarr_format: ZarrFormat, consolidated: bool
372372
group = zarr.api.synchronous.consolidate_metadata(
373373
store=store, zarr_format=zarr_format
374374
)
375-
else:
376-
if isinstance(store, ZipStore):
377-
with pytest.warns(UserWarning, match="Duplicate name: "):
378-
group = zarr.api.synchronous.consolidate_metadata(
379-
store=store, zarr_format=zarr_format
380-
)
381-
else:
375+
elif isinstance(store, ZipStore):
376+
with pytest.warns(UserWarning, match="Duplicate name: "):
382377
group = zarr.api.synchronous.consolidate_metadata(
383378
store=store, zarr_format=zarr_format
384379
)
380+
else:
381+
group = zarr.api.synchronous.consolidate_metadata(store=store, zarr_format=zarr_format)
385382
# we're going to assume that `group.metadata` is correct, and reuse that to focus
386383
# on indexing in this test. Other tests verify the correctness of group.metadata
387384
object.__setattr__(
@@ -581,12 +578,11 @@ def test_group_child_iterators(store: Store, zarr_format: ZarrFormat, consolidat
581578
group = zarr.consolidate_metadata(store)
582579
else:
583580
group = zarr.consolidate_metadata(store)
584-
else:
585-
if isinstance(store, ZipStore):
586-
with pytest.warns(UserWarning, match="Duplicate name: "):
587-
group = zarr.consolidate_metadata(store)
588-
else:
581+
elif isinstance(store, ZipStore):
582+
with pytest.warns(UserWarning, match="Duplicate name: "):
589583
group = zarr.consolidate_metadata(store)
584+
else:
585+
group = zarr.consolidate_metadata(store)
590586
if zarr_format == 2:
591587
metadata = {
592588
"subarray": {
@@ -1443,15 +1439,14 @@ async def test_members_name(store: Store, consolidate: bool, zarr_format: ZarrFo
14431439
group = zarr.api.synchronous.consolidate_metadata(store)
14441440
else:
14451441
group = zarr.api.synchronous.consolidate_metadata(store)
1446-
else:
1447-
if zarr_format == 3:
1448-
with pytest.warns(
1449-
ZarrUserWarning,
1450-
match="Consolidated metadata is currently not part in the Zarr format 3 specification.",
1451-
):
1452-
group = zarr.api.synchronous.consolidate_metadata(store)
1453-
else:
1442+
elif zarr_format == 3:
1443+
with pytest.warns(
1444+
ZarrUserWarning,
1445+
match="Consolidated metadata is currently not part in the Zarr format 3 specification.",
1446+
):
14541447
group = zarr.api.synchronous.consolidate_metadata(store)
1448+
else:
1449+
group = zarr.api.synchronous.consolidate_metadata(store)
14551450
result = group["a"]["b"]
14561451
assert result.name == "/a/b"
14571452

0 commit comments

Comments
 (0)