Skip to content

Commit 3b9f8ab

Browse files
Apply ruff/Pylint rule PLR5501
PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
1 parent 2d2bcd8 commit 3b9f8ab

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
@@ -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():
@@ -1273,9 +1272,8 @@ async def require_array(
12731272
if exact:
12741273
if ds.dtype != dtype:
12751274
raise TypeError(f"Incompatible dtype ({ds.dtype} vs {dtype})")
1276-
else:
1277-
if not np.can_cast(ds.dtype, dtype):
1278-
raise TypeError(f"Incompatible dtype ({ds.dtype} vs {dtype})")
1275+
elif not np.can_cast(ds.dtype, dtype):
1276+
raise TypeError(f"Incompatible dtype ({ds.dtype} vs {dtype})")
12791277
except KeyError:
12801278
ds = await self.create_array(name, shape=shape, dtype=dtype, **kwargs)
12811279

@@ -3119,24 +3117,23 @@ 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-
msg = f"A group exists in store {store!r} at path {key!r}."
3135-
raise ContainsGroupError(msg)
3136-
elif isinstance(extant_node, ArrayV2Metadata | ArrayV3Metadata):
3137-
# we are trying to overwrite an existing array. this is an error.
3138-
msg = f"An array exists in store {store!r} at path {key!r}."
3139-
raise ContainsArrayError(msg)
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+
msg = f"A group exists in store {store!r} at path {key!r}."
3132+
raise ContainsGroupError(msg)
3133+
elif isinstance(extant_node, ArrayV2Metadata | ArrayV3Metadata):
3134+
# we are trying to overwrite an existing array. this is an error.
3135+
msg = f"An array exists in store {store!r} at path {key!r}."
3136+
raise ContainsArrayError(msg)
31403137

31413138
nodes_explicit: dict[str, GroupMetadata | ArrayV2Metadata | ArrayV3Metadata] = {}
31423139

@@ -3296,13 +3293,12 @@ def _parse_hierarchy_dict(
32963293
# If a component is not already in the output dict, add ImplicitGroupMetadata
32973294
if subpath not in out:
32983295
out[subpath] = ImplicitGroupMarker(zarr_format=v.zarr_format)
3299-
else:
3300-
if not isinstance(out[subpath], GroupMetadata | ImplicitGroupMarker):
3301-
msg = (
3302-
f"The node at {subpath} contains other nodes, but it is not a Zarr group. "
3303-
"This is invalid. Only Zarr groups can contain other nodes."
3304-
)
3305-
raise ValueError(msg)
3296+
elif not isinstance(out[subpath], GroupMetadata | ImplicitGroupMarker):
3297+
msg = (
3298+
f"The node at {subpath} contains other nodes, but it is not a Zarr group. "
3299+
"This is invalid. Only Zarr groups can contain other nodes."
3300+
)
3301+
raise ValueError(msg)
33063302
return out
33073303

33083304

@@ -3510,12 +3506,11 @@ async def _read_metadata_v2(store: Store, path: str) -> ArrayV2Metadata | GroupM
35103506
# return the array metadata.
35113507
if zarray_bytes is not None:
35123508
zmeta = json.loads(zarray_bytes.to_bytes())
3509+
elif zgroup_bytes is None:
3510+
# neither .zarray or .zgroup were found results in KeyError
3511+
raise FileNotFoundError(path)
35133512
else:
3514-
if zgroup_bytes is None:
3515-
# neither .zarray or .zgroup were found results in KeyError
3516-
raise FileNotFoundError(path)
3517-
else:
3518-
zmeta = json.loads(zgroup_bytes.to_bytes())
3513+
zmeta = json.loads(zgroup_bytes.to_bytes())
35193514

35203515
return _build_metadata_v2(zmeta, zattrs)
35213516

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": {
@@ -1365,15 +1361,14 @@ async def test_members_name(store: Store, consolidate: bool, zarr_format: ZarrFo
13651361
group = zarr.api.synchronous.consolidate_metadata(store)
13661362
else:
13671363
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:
1364+
elif zarr_format == 3:
1365+
with pytest.warns(
1366+
ZarrUserWarning,
1367+
match="Consolidated metadata is currently not part in the Zarr format 3 specification.",
1368+
):
13761369
group = zarr.api.synchronous.consolidate_metadata(store)
1370+
else:
1371+
group = zarr.api.synchronous.consolidate_metadata(store)
13771372
result = group["a"]["b"]
13781373
assert result.name == "/a/b"
13791374

0 commit comments

Comments
 (0)