Skip to content

Commit 284e5e2

Browse files
committed
lint
1 parent 88a4875 commit 284e5e2

4 files changed

Lines changed: 63 additions & 56 deletions

File tree

src/zarr/codecs/sharding.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,9 @@ def _encode_shard_dict_sync(
544544
if self.index_location == ShardingCodecIndexLocation.start:
545545
empty_chunks_mask = index.offsets_and_lengths[..., 0] == MAX_UINT_64
546546
index.offsets_and_lengths[~empty_chunks_mask, 0] += len(index_bytes)
547-
index_bytes = self._encode_shard_index_sync(index) # encode again with corrected offsets
547+
index_bytes = self._encode_shard_index_sync(
548+
index
549+
) # encode again with corrected offsets
548550
buffers.insert(0, index_bytes)
549551
else:
550552
buffers.append(index_bytes)
@@ -966,10 +968,10 @@ def _decode_shard_index_sync(
966968
spec = bb.resolve_metadata(spec)
967969

968970
# Decode: reverse bb, then ab, then reverse aa
969-
chunk_bytes: Buffer | None = index_bytes
971+
chunk_bytes: Buffer = index_bytes
970972
for bb_codec, s in reversed(bb_with_spec):
971973
chunk_bytes = bb_codec._decode_sync(chunk_bytes, s)
972-
chunk_array = ab_codec._decode_sync(chunk_bytes, ab_spec)
974+
chunk_array: NDBuffer = ab_codec._decode_sync(chunk_bytes, ab_spec)
973975
for aa_codec, s in reversed(aa_with_spec):
974976
chunk_array = aa_codec._decode_sync(chunk_array, s)
975977

@@ -984,18 +986,19 @@ def _encode_shard_index_sync(self, index: _ShardIndex) -> Buffer:
984986

985987
aa_codecs, ab_codec, bb_codecs = codecs_from_list(list(self.index_codecs))
986988

987-
chunk_array: NDBuffer | None = get_ndbuffer_class().from_numpy_array(
988-
index.offsets_and_lengths
989-
)
989+
aa_out: NDBuffer | None = get_ndbuffer_class().from_numpy_array(index.offsets_and_lengths)
990990

991991
# Encode: aa forward, then ab, then bb forward
992992
spec = index_chunk_spec
993993
for aa_codec in aa_codecs:
994-
chunk_array = aa_codec._encode_sync(chunk_array, spec)
994+
assert aa_out is not None
995+
aa_out = aa_codec._encode_sync(aa_out, spec)
995996
spec = aa_codec.resolve_metadata(spec)
996-
chunk_bytes = ab_codec._encode_sync(chunk_array, spec)
997+
assert aa_out is not None
998+
chunk_bytes = ab_codec._encode_sync(aa_out, spec)
997999
spec = ab_codec.resolve_metadata(spec)
9981000
for bb_codec in bb_codecs:
1001+
assert chunk_bytes is not None
9991002
chunk_bytes = bb_codec._encode_sync(chunk_bytes, spec)
10001003
spec = bb_codec.resolve_metadata(spec)
10011004

src/zarr/core/array.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,9 +1993,8 @@ def _can_use_sync_path(self) -> bool:
19931993
"""
19941994
pipeline = self.async_array.codec_pipeline
19951995
store_path = self.async_array.store_path
1996-
return (
1997-
getattr(pipeline, "supports_sync_io", False)
1998-
and getattr(store_path, "supports_sync", False)
1996+
return getattr(pipeline, "supports_sync_io", False) and getattr(
1997+
store_path, "supports_sync", False
19991998
)
20001999

20012000
@classmethod

src/zarr/core/codec_pipeline.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,22 @@ def _encode_one(
322322
return None
323323

324324
spec = chunk_spec
325+
aa_out: NDBuffer | None = chunk_array
325326

326327
for aa_codec in self.array_array_codecs:
327-
chunk_array = aa_codec._encode_sync(chunk_array, spec)
328+
if aa_out is None:
329+
return None
330+
aa_out = aa_codec._encode_sync(aa_out, spec)
328331
spec = aa_codec.resolve_metadata(spec)
329332

330-
chunk_bytes = self.array_bytes_codec._encode_sync(chunk_array, spec)
333+
if aa_out is None:
334+
return None
335+
chunk_bytes = self.array_bytes_codec._encode_sync(aa_out, spec)
331336
spec = self.array_bytes_codec.resolve_metadata(spec)
332337

333338
for bb_codec in self.bytes_bytes_codecs:
339+
if chunk_bytes is None:
340+
return None
334341
chunk_bytes = bb_codec._encode_sync(chunk_bytes, spec)
335342
spec = bb_codec.resolve_metadata(spec)
336343

@@ -694,7 +701,7 @@ async def _write_batch_compute(
694701
def _merge_and_filter(
695702
self,
696703
chunk_array_decoded: Iterable[NDBuffer | None],
697-
batch_info: list,
704+
batch_info: list[tuple[Any, ArraySpec, SelectorTuple, SelectorTuple, bool]],
698705
value: NDBuffer,
699706
drop_axes: tuple[int, ...],
700707
) -> list[NDBuffer | None]:
@@ -717,9 +724,7 @@ def _merge_and_filter(
717724
) in zip(chunk_array_decoded, batch_info, strict=False)
718725
]
719726
chunk_array_batch: list[NDBuffer | None] = []
720-
for chunk_array, (_, chunk_spec, *_) in zip(
721-
chunk_array_merged, batch_info, strict=False
722-
):
727+
for chunk_array, (_, chunk_spec, *_) in zip(chunk_array_merged, batch_info, strict=False):
723728
if chunk_array is None:
724729
chunk_array_batch.append(None) # type: ignore[unreachable]
725730
else:

tests/test_sync_codec_pipeline.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
from typing import Any
6+
57
import numpy as np
68
import pytest
79

@@ -17,9 +19,7 @@
1719
from zarr.storage import MemoryStore
1820

1921

20-
def _make_array_spec(
21-
shape: tuple[int, ...], dtype: np.dtype
22-
) -> ArraySpec:
22+
def _make_array_spec(shape: tuple[int, ...], dtype: np.dtype[Any]) -> ArraySpec:
2323
zdtype = get_data_type_from_native_dtype(dtype)
2424
return ArraySpec(
2525
shape=shape,
@@ -30,7 +30,7 @@ def _make_array_spec(
3030
)
3131

3232

33-
def _make_nd_buffer(arr: np.ndarray) -> zarr.core.buffer.NDBuffer:
33+
def _make_nd_buffer(arr: np.ndarray[Any, Any]) -> zarr.core.buffer.NDBuffer:
3434
return default_buffer_prototype().nd_buffer.from_numpy_array(arr)
3535

3636

@@ -40,19 +40,19 @@ def _make_nd_buffer(arr: np.ndarray) -> zarr.core.buffer.NDBuffer:
4040

4141

4242
class TestSupportsSync:
43-
def test_gzip_supports_sync(self):
43+
def test_gzip_supports_sync(self) -> None:
4444
assert GzipCodec().supports_sync
4545

46-
def test_zstd_supports_sync(self):
46+
def test_zstd_supports_sync(self) -> None:
4747
assert ZstdCodec().supports_sync
4848

49-
def test_bytes_supports_sync(self):
49+
def test_bytes_supports_sync(self) -> None:
5050
assert BytesCodec().supports_sync
5151

52-
def test_transpose_supports_sync(self):
52+
def test_transpose_supports_sync(self) -> None:
5353
assert TransposeCodec(order=(0, 1)).supports_sync
5454

55-
def test_sharding_supports_sync(self):
55+
def test_sharding_supports_sync(self) -> None:
5656
from zarr.codecs.sharding import ShardingCodec
5757

5858
assert ShardingCodec(chunk_shape=(8,)).supports_sync
@@ -64,7 +64,7 @@ def test_sharding_supports_sync(self):
6464

6565

6666
class TestGzipCodecSync:
67-
def test_roundtrip(self):
67+
def test_roundtrip(self) -> None:
6868
codec = GzipCodec(level=1)
6969
arr = np.arange(100, dtype="float64")
7070
spec = _make_array_spec(arr.shape, arr.dtype)
@@ -78,7 +78,7 @@ def test_roundtrip(self):
7878

7979

8080
class TestZstdCodecSync:
81-
def test_roundtrip(self):
81+
def test_roundtrip(self) -> None:
8282
codec = ZstdCodec(level=1)
8383
arr = np.arange(100, dtype="float64")
8484
spec = _make_array_spec(arr.shape, arr.dtype)
@@ -92,7 +92,7 @@ def test_roundtrip(self):
9292

9393

9494
class TestBytesCodecSync:
95-
def test_roundtrip(self):
95+
def test_roundtrip(self) -> None:
9696
codec = BytesCodec()
9797
arr = np.arange(100, dtype="float64")
9898
spec = _make_array_spec(arr.shape, arr.dtype)
@@ -108,7 +108,7 @@ def test_roundtrip(self):
108108

109109

110110
class TestTransposeCodecSync:
111-
def test_roundtrip(self):
111+
def test_roundtrip(self) -> None:
112112
codec = TransposeCodec(order=(1, 0))
113113
arr = np.arange(12, dtype="float64").reshape(3, 4)
114114
spec = _make_array_spec(arr.shape, arr.dtype)
@@ -127,29 +127,31 @@ def test_roundtrip(self):
127127

128128

129129
class TestPipelineConstruction:
130-
def test_from_codecs_valid(self):
130+
def test_from_codecs_valid(self) -> None:
131131
pipeline = BatchedCodecPipeline.from_codecs([BytesCodec(), GzipCodec(level=1)])
132132
assert isinstance(pipeline, BatchedCodecPipeline)
133133
assert len(pipeline.bytes_bytes_codecs) == 1
134134
assert isinstance(pipeline.array_bytes_codec, BytesCodec)
135135

136-
def test_from_codecs_accepts_sharding(self):
136+
def test_from_codecs_accepts_sharding(self) -> None:
137137
from zarr.codecs.sharding import ShardingCodec
138138

139139
pipeline = BatchedCodecPipeline.from_codecs([ShardingCodec(chunk_shape=(8,))])
140140
assert isinstance(pipeline, BatchedCodecPipeline)
141141
assert pipeline._all_sync
142142

143-
def test_from_codecs_rejects_missing_array_bytes(self):
143+
def test_from_codecs_rejects_missing_array_bytes(self) -> None:
144144
with pytest.raises(ValueError, match="Required ArrayBytesCodec"):
145145
BatchedCodecPipeline.from_codecs([GzipCodec()])
146146

147-
def test_from_codecs_with_transpose(self):
148-
pipeline = BatchedCodecPipeline.from_codecs([
149-
TransposeCodec(order=(1, 0)),
150-
BytesCodec(),
151-
GzipCodec(level=1),
152-
])
147+
def test_from_codecs_with_transpose(self) -> None:
148+
pipeline = BatchedCodecPipeline.from_codecs(
149+
[
150+
TransposeCodec(order=(1, 0)),
151+
BytesCodec(),
152+
GzipCodec(level=1),
153+
]
154+
)
153155
assert len(pipeline.array_array_codecs) == 1
154156
assert isinstance(pipeline.array_array_codecs[0], TransposeCodec)
155157

@@ -161,21 +163,21 @@ def test_from_codecs_with_transpose(self):
161163

162164
class TestPipelineRoundtrip:
163165
@pytest.mark.asyncio
164-
async def test_encode_decode_single_chunk(self):
166+
async def test_encode_decode_single_chunk(self) -> None:
165167
pipeline = BatchedCodecPipeline.from_codecs([BytesCodec(), GzipCodec(level=1)])
166168
arr = np.random.default_rng(42).standard_normal((32, 32)).astype("float64")
167169
spec = _make_array_spec(arr.shape, arr.dtype)
168170
pipeline = pipeline.evolve_from_array_spec(spec)
169171
nd_buf = _make_nd_buffer(arr)
170172

171173
encoded = await pipeline.encode([(nd_buf, spec)])
172-
decoded = await pipeline.decode([(list(encoded)[0], spec)])
173-
result = list(decoded)[0]
174+
decoded = await pipeline.decode([(next(iter(encoded)), spec)])
175+
result = next(iter(decoded))
174176
assert result is not None
175177
np.testing.assert_array_equal(arr, result.as_numpy_array())
176178

177179
@pytest.mark.asyncio
178-
async def test_encode_decode_multiple_chunks(self):
180+
async def test_encode_decode_multiple_chunks(self) -> None:
179181
pipeline = BatchedCodecPipeline.from_codecs([BytesCodec(), GzipCodec(level=1)])
180182
rng = np.random.default_rng(42)
181183
spec = _make_array_spec((16, 16), np.dtype("float64"))
@@ -185,20 +187,20 @@ async def test_encode_decode_multiple_chunks(self):
185187

186188
encoded = list(await pipeline.encode([(buf, spec) for buf in nd_bufs]))
187189
decoded = list(await pipeline.decode([(enc, spec) for enc in encoded]))
188-
for original, dec in zip(chunks, decoded):
190+
for original, dec in zip(chunks, decoded, strict=False):
189191
assert dec is not None
190192
np.testing.assert_array_equal(original, dec.as_numpy_array())
191193

192194
@pytest.mark.asyncio
193-
async def test_encode_decode_empty_batch(self):
195+
async def test_encode_decode_empty_batch(self) -> None:
194196
pipeline = BatchedCodecPipeline.from_codecs([BytesCodec(), GzipCodec(level=1)])
195197
encoded = await pipeline.encode([])
196198
assert list(encoded) == []
197199
decoded = await pipeline.decode([])
198200
assert list(decoded) == []
199201

200202
@pytest.mark.asyncio
201-
async def test_encode_decode_none_chunk(self):
203+
async def test_encode_decode_none_chunk(self) -> None:
202204
pipeline = BatchedCodecPipeline.from_codecs([BytesCodec(), GzipCodec(level=1)])
203205
spec = _make_array_spec((8,), np.dtype("float64"))
204206
pipeline = pipeline.evolve_from_array_spec(spec)
@@ -216,7 +218,7 @@ async def test_encode_decode_none_chunk(self):
216218

217219

218220
class TestDefaultPipelineSync:
219-
def test_create_array_uses_batched_pipeline(self):
221+
def test_create_array_uses_batched_pipeline(self) -> None:
220222
store = MemoryStore()
221223
arr = zarr.create_array(
222224
store,
@@ -230,7 +232,7 @@ def test_create_array_uses_batched_pipeline(self):
230232
arr[:] = data
231233
np.testing.assert_array_equal(arr[:], data)
232234

233-
def test_open_uses_batched_pipeline(self):
235+
def test_open_uses_batched_pipeline(self) -> None:
234236
store = MemoryStore()
235237
arr = zarr.create_array(
236238
store,
@@ -245,7 +247,7 @@ def test_open_uses_batched_pipeline(self):
245247
assert isinstance(arr2.async_array.codec_pipeline, BatchedCodecPipeline)
246248
np.testing.assert_array_equal(arr2[:], data)
247249

248-
def test_from_array_uses_batched_pipeline(self):
250+
def test_from_array_uses_batched_pipeline(self) -> None:
249251
store1 = MemoryStore()
250252
arr1 = zarr.create_array(
251253
store1,
@@ -261,7 +263,7 @@ def test_from_array_uses_batched_pipeline(self):
261263
assert isinstance(arr2.async_array.codec_pipeline, BatchedCodecPipeline)
262264
np.testing.assert_array_equal(arr2[:], data)
263265

264-
def test_partial_write(self):
266+
def test_partial_write(self) -> None:
265267
store = MemoryStore()
266268
arr = zarr.create_array(
267269
store,
@@ -276,7 +278,7 @@ def test_partial_write(self):
276278
expected[5:15] = np.arange(10, dtype="int32") + 1
277279
np.testing.assert_array_equal(result, expected)
278280

279-
def test_zstd_codec(self):
281+
def test_zstd_codec(self) -> None:
280282
store = MemoryStore()
281283
arr = zarr.create_array(
282284
store,
@@ -289,18 +291,16 @@ def test_zstd_codec(self):
289291
arr[:] = data
290292
np.testing.assert_array_equal(arr[:], data)
291293

292-
def test_supports_sync_io(self):
294+
def test_supports_sync_io(self) -> None:
293295
"""Default pipeline supports sync IO when all codecs are sync."""
294296
pipeline = BatchedCodecPipeline.from_codecs([BytesCodec(), GzipCodec(level=1)])
295297
assert pipeline.supports_sync_io
296298

297-
def test_config_switch_to_sync_pipeline_compat(self):
299+
def test_config_switch_to_sync_pipeline_compat(self) -> None:
298300
"""Verify backwards compat: SyncCodecPipeline config path still works."""
299301
from zarr.experimental.sync_codecs import SyncCodecPipeline
300302

301-
zarr.config.set(
302-
{"codec_pipeline.path": "zarr.experimental.sync_codecs.SyncCodecPipeline"}
303-
)
303+
zarr.config.set({"codec_pipeline.path": "zarr.experimental.sync_codecs.SyncCodecPipeline"})
304304
try:
305305
store = MemoryStore()
306306
arr = zarr.create_array(store, shape=(10,), dtype="float64")

0 commit comments

Comments
 (0)