Skip to content

Commit ddace47

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

File tree

3 files changed

+60
-69
lines changed

3 files changed

+60
-69
lines changed

src/zarr/core/codec_pipeline.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,12 @@ async def _read_key(
406406
):
407407
if chunk_array is None:
408408
chunk_array_batch.append(None) # type: ignore[unreachable]
409+
elif 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)
409413
else:
410-
if not chunk_spec.config.write_empty_chunks and chunk_array.all_equal(
411-
fill_value_or_default(chunk_spec)
412-
):
413-
chunk_array_batch.append(None)
414-
else:
415-
chunk_array_batch.append(chunk_array)
414+
chunk_array_batch.append(chunk_array)
416415

417416
chunk_bytes_batch = await self.encode_batch(
418417
[

src/zarr/core/group.py

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/test_group.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -372,16 +372,15 @@ 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(
382+
store=store, zarr_format=zarr_format
383+
)
385384
# we're going to assume that `group.metadata` is correct, and reuse that to focus
386385
# on indexing in this test. Other tests verify the correctness of group.metadata
387386
object.__setattr__(
@@ -581,12 +580,11 @@ def test_group_child_iterators(store: Store, zarr_format: ZarrFormat, consolidat
581580
group = zarr.consolidate_metadata(store)
582581
else:
583582
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:
583+
elif isinstance(store, ZipStore):
584+
with pytest.warns(UserWarning, match="Duplicate name: "):
589585
group = zarr.consolidate_metadata(store)
586+
else:
587+
group = zarr.consolidate_metadata(store)
590588
if zarr_format == 2:
591589
metadata = {
592590
"subarray": {
@@ -1365,15 +1363,14 @@ async def test_members_name(store: Store, consolidate: bool, zarr_format: ZarrFo
13651363
group = zarr.api.synchronous.consolidate_metadata(store)
13661364
else:
13671365
group = zarr.api.synchronous.consolidate_metadata(store)
1368-
else:
1369-
if zarr_format == 3:
1370-
with pytest.warns(
1371-
ZarrUserWarning,
1372-
match="Consolidated metadata is currently not part in the Zarr format 3 specification.",
1373-
):
1374-
group = zarr.api.synchronous.consolidate_metadata(store)
1375-
else:
1366+
elif zarr_format == 3:
1367+
with pytest.warns(
1368+
ZarrUserWarning,
1369+
match="Consolidated metadata is currently not part in the Zarr format 3 specification.",
1370+
):
13761371
group = zarr.api.synchronous.consolidate_metadata(store)
1372+
else:
1373+
group = zarr.api.synchronous.consolidate_metadata(store)
13771374
result = group["a"]["b"]
13781375
assert result.name == "/a/b"
13791376

0 commit comments

Comments
 (0)