Skip to content

Commit 7db1c74

Browse files
authored
chore(tests): move gzip mtime-comparison helper out of library code (#4326)
* chore(tests): move gzip mtime-comparison helper out of library code `_gzip_streams_equal_except_mtime` was only used by tests but lived in `zarr.codecs.gzip`. Move it to `tests/conftest.py` alongside the other shared test helpers (`nan_equal`, `deep_nan_equal`), rename it to `gzip_streams_equal_except_mtime`, and update both importers. Assisted-by: ClaudeCode:claude-fable-5-1 * chore: rename changelog fragment to the PR number Assisted-by: ClaudeCode:claude-fable-5-1
1 parent 542ceba commit 7db1c74

5 files changed

Lines changed: 24 additions & 23 deletions

File tree

‎changes/4326.misc.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Removed the private test helper `_gzip_streams_equal_except_mtime` from `zarr.codecs.gzip`; it now lives in the test suite.

‎src/zarr/codecs/gzip.py‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@ def parse_gzip_level(data: JSON) -> int:
2828
return parsed
2929

3030

31-
def _gzip_streams_equal_except_mtime(a: bytes, b: bytes) -> bool:
32-
"""Compare two gzip streams, ignoring the MTIME field of the header.
33-
34-
Per RFC 1952 the gzip header is [magic(2)][CM(1)][FLG(1)][MTIME(4)][XFL(1)][OS(1)],
35-
so bytes 4-8 are MTIME. The fixed offsets assume the standard 10-byte header
36-
with no FNAME/FEXTRA/FCOMMENT flags set, which holds here because numcodecs'
37-
``GZip.encode`` wraps ``gzip.GzipFile`` without a filename.
38-
"""
39-
if len(a) != len(b):
40-
return False
41-
42-
return a[:4] == b[:4] and a[8:] == b[8:]
43-
44-
4531
@dataclass(frozen=True)
4632
class GzipCodec(BytesBytesCodec):
4733
"""gzip codec"""

‎tests/conftest.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,20 @@ def deep_nan_equal(a: object, b: object) -> bool:
544544
return nan_equal(a, b)
545545

546546

547+
def gzip_streams_equal_except_mtime(a: bytes, b: bytes) -> bool:
548+
"""Compare two gzip streams, ignoring the MTIME field of the header.
549+
550+
Per RFC 1952 the gzip header is [magic(2)][CM(1)][FLG(1)][MTIME(4)][XFL(1)][OS(1)],
551+
so bytes 4-8 are MTIME. The fixed offsets assume the standard 10-byte header
552+
with no FNAME/FEXTRA/FCOMMENT flags set, which holds here because numcodecs'
553+
``GZip.encode`` wraps ``gzip.GzipFile`` without a filename.
554+
"""
555+
if len(a) != len(b):
556+
return False
557+
558+
return a[:4] == b[:4] and a[8:] == b[8:]
559+
560+
547561
# Shared mock-S3 (moto) backend. A single server is reused across the whole test session by
548562
# every test that needs S3 -- both the fsspec store tests and the documentation examples --
549563
# instead of each module standing up its own. Consumers create their own buckets and choose

‎tests/test_codecs/test_gzip.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import pytest
33

44
import zarr
5+
from tests.conftest import gzip_streams_equal_except_mtime
56
from zarr.abc.codec import SupportsSyncCodec
67
from zarr.abc.store import Store
78
from zarr.codecs import GzipCodec
8-
from zarr.codecs.gzip import _gzip_streams_equal_except_mtime
99
from zarr.core.array_spec import ArrayConfig, ArraySpec
1010
from zarr.core.buffer import default_buffer_prototype
1111
from zarr.core.dtype import get_data_type_from_native_dtype
@@ -59,31 +59,31 @@ def test_gzip_streams_equal_except_mtime() -> None:
5959
suffix = b"\x00\xff\x10\x20"
6060

6161
# Identical streams are equal.
62-
assert _gzip_streams_equal_except_mtime(
62+
assert gzip_streams_equal_except_mtime(
6363
prefix + mtime + suffix,
6464
prefix + mtime + suffix,
6565
)
6666

6767
# Streams with different MTIME values are still equal.
68-
assert _gzip_streams_equal_except_mtime(
68+
assert gzip_streams_equal_except_mtime(
6969
prefix + b"\x01\x02\x03\x04" + suffix,
7070
prefix + b"\x05\x06\x07\x08" + suffix,
7171
)
7272

7373
# Differences after MTIME are detected.
74-
assert not _gzip_streams_equal_except_mtime(
74+
assert not gzip_streams_equal_except_mtime(
7575
prefix + mtime + suffix,
7676
prefix + mtime + b"\x00\xff\x10\x21",
7777
)
7878

7979
# Differences before MTIME are detected.
80-
assert not _gzip_streams_equal_except_mtime(
80+
assert not gzip_streams_equal_except_mtime(
8181
prefix + mtime + suffix,
8282
b"\x1f\x8b\x09\x00" + mtime + suffix,
8383
)
8484

8585
# Different lengths are detected.
86-
assert not _gzip_streams_equal_except_mtime(
86+
assert not gzip_streams_equal_except_mtime(
8787
prefix + mtime + suffix,
8888
prefix + mtime + suffix + b"\x00",
8989
)

‎tests/test_fused_pipeline.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pytest
1212

1313
import zarr
14-
from tests.conftest import Expect
14+
from tests.conftest import Expect, gzip_streams_equal_except_mtime
1515
from zarr.abc.codec import (
1616
ArrayBytesCodec,
1717
ArrayBytesCodecPartialDecodeMixin,
@@ -20,7 +20,7 @@
2020
)
2121
from zarr.abc.store import Store, _store_supports_sync_io
2222
from zarr.codecs.bytes import BytesCodec
23-
from zarr.codecs.gzip import GzipCodec, _gzip_streams_equal_except_mtime
23+
from zarr.codecs.gzip import GzipCodec
2424
from zarr.codecs.transpose import TransposeCodec
2525
from zarr.codecs.zstd import ZstdCodec
2626
from zarr.core.array_spec import ArrayConfig, ArraySpec
@@ -785,7 +785,7 @@ def _bytes_identical(a: bytes, b: bytes) -> bool:
785785
),
786786
Expect(
787787
input=(BytesCodec(), GzipCodec(level=1)),
788-
output=_gzip_streams_equal_except_mtime,
788+
output=gzip_streams_equal_except_mtime,
789789
id="bb",
790790
),
791791
Expect(

0 commit comments

Comments
 (0)