Skip to content

Commit b3010fc

Browse files
Merge collapsible if statements (#1999)
1 parent 61b9404 commit b3010fc

File tree

5 files changed

+23
-25
lines changed

5 files changed

+23
-25
lines changed

src/zarr/codecs/bytes.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,15 @@ async def _encode_single(
9797
chunk_spec: ArraySpec,
9898
) -> Buffer | None:
9999
assert isinstance(chunk_array, NDBuffer)
100-
if chunk_array.dtype.itemsize > 1:
101-
if self.endian is not None and self.endian != chunk_array.byteorder:
102-
# type-ignore is a numpy bug
103-
# see https://github.com/numpy/numpy/issues/26473
104-
new_dtype = chunk_array.dtype.newbyteorder(self.endian.name) # type: ignore[arg-type]
105-
chunk_array = chunk_array.astype(new_dtype)
100+
if (
101+
chunk_array.dtype.itemsize > 1
102+
and self.endian is not None
103+
and self.endian != chunk_array.byteorder
104+
):
105+
# type-ignore is a numpy bug
106+
# see https://github.com/numpy/numpy/issues/26473
107+
new_dtype = chunk_array.dtype.newbyteorder(self.endian.name) # type: ignore[arg-type]
108+
chunk_array = chunk_array.astype(new_dtype)
106109

107110
nd_array = chunk_array.as_ndarray_like()
108111
# Flatten the nd-array (only copy if needed) and reinterpret as bytes

src/zarr/codecs/pipeline.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,7 @@ def _merge_chunk_array(
315315
)
316316
else:
317317
chunk_array = existing_chunk_array.copy() # make a writable copy
318-
if chunk_selection == ():
319-
chunk_value = value
320-
elif is_scalar(value.as_ndarray_like(), chunk_spec.dtype):
318+
if chunk_selection == () or is_scalar(value.as_ndarray_like(), chunk_spec.dtype):
321319
chunk_value = value
322320
else:
323321
chunk_value = value[out_selection]

src/zarr/codecs/registry.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ def register_codec(key: str, codec_cls: type[Codec]) -> None:
2525

2626
def get_codec_class(key: str) -> type[Codec]:
2727
item = __codec_registry.get(key)
28-
if item is None:
29-
if key in __lazy_load_codecs:
30-
# logger.debug("Auto loading codec '%s' from entrypoint", codec_id)
31-
cls = __lazy_load_codecs[key].load()
32-
register_codec(key, cls)
33-
item = __codec_registry.get(key)
28+
if item is None and key in __lazy_load_codecs:
29+
# logger.debug("Auto loading codec '%s' from entrypoint", codec_id)
30+
cls = __lazy_load_codecs[key].load()
31+
register_codec(key, cls)
32+
item = __codec_registry.get(key)
3433
if item:
3534
return item
3635
raise KeyError(key)

src/zarr/v2/convenience.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -680,12 +680,11 @@ def copy_store(
680680

681681
# decide what to do
682682
do_copy = True
683-
if if_exists != "replace":
684-
if dest_key in dest:
685-
if if_exists == "raise":
686-
raise CopyError("key {!r} exists in destination".format(dest_key))
687-
elif if_exists == "skip":
688-
do_copy = False
683+
if if_exists != "replace" and dest_key in dest:
684+
if if_exists == "raise":
685+
raise CopyError("key {!r} exists in destination".format(dest_key))
686+
elif if_exists == "skip":
687+
do_copy = False
689688

690689
# take action
691690
if do_copy:

src/zarr/v2/util.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,9 @@ def __init__(self, obj, depth=0, level=None):
428428
self.level = level
429429

430430
def get_children(self):
431-
if hasattr(self.obj, "values"):
432-
if self.level is None or self.depth < self.level:
433-
depth = self.depth + 1
434-
return [TreeNode(o, depth=depth, level=self.level) for o in self.obj.values()]
431+
if hasattr(self.obj, "values") and (self.level is None or self.depth < self.level):
432+
depth = self.depth + 1
433+
return [TreeNode(o, depth=depth, level=self.level) for o in self.obj.values()]
435434
return []
436435

437436
def get_text(self):

0 commit comments

Comments
 (0)