From ff36c016cebef004d4b896609a951daa854d24b8 Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Sat, 21 Nov 2026 13:48:18 +0100 Subject: [PATCH 1/2] test: check the Codec contract for every codec and add static type tests Add tests/test_codec_contract.py, which runs every registered codec through the behavior the Codec base class promises: encode and decode return buffers, decode(out=...) returns the caller's out, and get_config round-trips through from_config. Known violations are strict xfails, so fixing one forces its entry to be removed. Add tests/typing, where assert_type checks (verified by mypy and pyright in strict mode) sit next to runtime assertions (verified by pytest), and a `typing` CI job that runs both checkers plus the repository's own mypy configuration. The type checkers are pinned in a new `typing` dependency group. Assisted-by: ClaudeCode:claude-opus-5-5 --- .github/workflows/ci.yaml | 37 ++++++ docs/release.md | 3 + pyproject.toml | 4 + tests/test_codec_contract.py | 203 ++++++++++++++++++++++++++++++ tests/typing/README.md | 25 ++++ tests/typing/__init__.py | 0 tests/typing/mypy.ini | 15 +++ tests/typing/pyrightconfig.json | 14 +++ tests/typing/test_compat_types.py | 31 +++++ 9 files changed, 332 insertions(+) create mode 100644 tests/test_codec_contract.py create mode 100644 tests/typing/README.md create mode 100644 tests/typing/__init__.py create mode 100644 tests/typing/mypy.ini create mode 100644 tests/typing/pyrightconfig.json create mode 100644 tests/typing/test_compat_types.py diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9f6583327..bec8fb69b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -125,6 +125,43 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} verbose: true + typing: + # Static types: the repository's own mypy configuration over src and tests, and the + # strict type tests in tests/typing under both mypy and pyright (see its README). + runs-on: ubuntu-latest + + defaults: + run: + shell: bash -el {0} + + steps: + - name: Checkout source + uses: actions/checkout@v6 + with: + submodules: recursive + fetch-depth: 0 # required for version resolution + + - name: Set up Python + uses: actions/setup-python@v6 + with: + python-version: "3.13" + cache: 'pip' + + - name: Install numcodecs and the type checkers + run: | + python -m pip install --upgrade pip # --group needs pip >= 25.1 + python -m pip install -v ".[test,msgpack,crc32c]" + python -m pip install --group typing + + - name: mypy (repository configuration) + run: mypy --config-file pyproject.toml src tests + + - name: mypy (strict type tests) + run: mypy --config-file tests/typing/mypy.ini tests/typing + + - name: pyright (strict type tests) + run: pyright -p tests/typing + test-zarr: runs-on: ubuntu-latest strategy: diff --git a/docs/release.md b/docs/release.md index b54747679..6847e8ea6 100644 --- a/docs/release.md +++ b/docs/release.md @@ -29,6 +29,9 @@ wheels) rather than every branch, test the oldest supported and latest Zarr releases, use Alpine's numpy in the i386 job, and remove the unused release-notes labeler. +* Check the behavior every codec promises through `numcodecs.abc.Codec` in a new + `tests/test_codec_contract.py`, and add strict static type tests in `tests/typing`, run by + mypy and pyright in a new `typing` CI job. ### Fixes diff --git a/pyproject.toml b/pyproject.toml index f71320993..721d18125 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -90,6 +90,10 @@ dev = [ "pytest==9.0.3", "pytest-cov", ] +typing = [ + "mypy==2.4.1", + "pyright==1.1.414", +] test-zarr-313 = [ {include-group="dev"}, "zarr==3.1.3", diff --git a/tests/test_codec_contract.py b/tests/test_codec_contract.py new file mode 100644 index 000000000..c328a25cc --- /dev/null +++ b/tests/test_codec_contract.py @@ -0,0 +1,203 @@ +"""The behavior every codec promises through `numcodecs.abc.Codec`, checked for every codec. + +These tests pin down the contract that the type annotations on `Codec` describe: + +- `encode` and `decode` return a buffer-like object (one that exports the buffer protocol). +- `decode(buf, out=out)` writes into `out` and returns that same object. +- `get_config()` holds an `id` and JSON-serializable values, and `from_config` rebuilds an + equal codec from it once `id` is removed. +""" + +from __future__ import annotations + +import json +import warnings +from collections.abc import Callable +from dataclasses import dataclass + +import numpy as np +import pytest + +import numcodecs +from numcodecs import pickles +from numcodecs.abc import Codec + + +@dataclass(frozen=True) +class Case: + """A codec and an array it can round-trip.""" + + make: Callable[[], Codec] + data: np.ndarray + + @property + def is_object(self) -> bool: + return self.data.dtype == object + + +f8 = np.linspace(0, 100, 16, dtype="f8") +obj_str = np.array(["foo", "bar", "baz", "qux"], dtype=object) + +CASES: dict[str, Case] = { + "zlib": Case(lambda: numcodecs.Zlib(), f8), + "gzip": Case(lambda: numcodecs.GZip(), f8), + "bz2": Case(lambda: numcodecs.BZ2(), f8), + "lzma": Case(lambda: numcodecs.LZMA(), f8), + "blosc": Case(lambda: numcodecs.Blosc(), f8), + "zstd": Case(lambda: numcodecs.Zstd(), f8), + "lz4": Case(lambda: numcodecs.LZ4(), f8), + "base64": Case(lambda: numcodecs.Base64(), f8), + "shuffle": Case(lambda: numcodecs.Shuffle(elementsize=8), f8), + "crc32": Case(lambda: numcodecs.CRC32(), f8), + "adler32": Case(lambda: numcodecs.Adler32(), f8), + "jenkins_lookup3": Case(lambda: numcodecs.JenkinsLookup3(), f8), + "fletcher32": Case(lambda: numcodecs.Fletcher32(), f8), + "astype": Case(lambda: numcodecs.AsType(encode_dtype="f4", decode_dtype="f8"), f8), + "delta": Case(lambda: numcodecs.Delta(dtype="i4"), np.arange(16, dtype="i4")), + "quantize": Case(lambda: numcodecs.Quantize(digits=1, dtype="f8"), f8), + "fixedscaleoffset": Case( + lambda: numcodecs.FixedScaleOffset(offset=0, scale=10, dtype="f8", astype="i4"), f8 + ), + "bitround": Case(lambda: numcodecs.BitRound(keepbits=10), f8), + "packbits": Case(lambda: numcodecs.PackBits(), np.array([True, False] * 8)), + "categorize": Case( + lambda: numcodecs.Categorize(labels=["foo", "bar", "baz", "qux"], dtype=object), obj_str + ), + "pickle": Case(lambda: numcodecs.Pickle(), obj_str), + "json2": Case(lambda: numcodecs.JSON(), obj_str), + "vlen-utf8": Case(lambda: numcodecs.VLenUTF8(), obj_str), + "vlen-bytes": Case( + lambda: numcodecs.VLenBytes(), np.array([b"foo", b"bar", b"baz", b"qux"], dtype=object) + ), + "vlen-array": Case( + lambda: numcodecs.VLenArray(dtype="i4"), + np.array([np.arange(3, dtype="i4"), np.arange(2, dtype="i4")] * 2, dtype=object), + ), + "msgpack2": Case(lambda: numcodecs.MsgPack(), obj_str), + "zfpy": Case(lambda: numcodecs.ZFPY(), f8), + "pcodec": Case(lambda: numcodecs.PCodec(), f8), + "crc32c": Case(lambda: numcodecs.CRC32C(), f8), +} + +# Codecs whose decode(..., out=bytearray) cannot work, because the codec needs `out` to carry +# a dtype: PCodec decodes straight into a typed array. +NO_BYTEARRAY_OUT = frozenset({"pcodec"}) + +# Known violations of the `out` contract. Each entry is a bug; remove it when fixed. +# `strict=True` makes a fixed entry fail, so this list cannot go stale. +OUT_NDARRAY_XFAIL: set[str] = { + "zlib", + "bz2", + "lzma", + "base64", + "shuffle", + "zfpy", + "pcodec", + "vlen-utf8", + "vlen-bytes", + "vlen-array", +} +OUT_BYTEARRAY_XFAIL: set[str] = { + "zlib", + "bz2", + "lzma", + "base64", + "shuffle", + "crc32", + "adler32", + "crc32c", + "jenkins_lookup3", + "astype", + "delta", + "quantize", + "fixedscaleoffset", + "bitround", + "packbits", + "zfpy", +} +DECODE_RETURNS_BUFFER_XFAIL: set[str] = set() + + +def _codec(codec_id: str) -> Codec: + case = CASES[codec_id] + try: + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + return case.make() + except AttributeError: + pytest.skip(f"{codec_id} is not available (optional dependency missing)") + + +def _params( + xfail: set[str], *, skip: frozenset[str] = frozenset(), objects: bool = True +) -> list[object]: + params: list[object] = [] + for codec_id, case in CASES.items(): + if codec_id in skip or (case.is_object and not objects): + continue + marks = [pytest.mark.xfail(strict=True, reason="known bug")] if codec_id in xfail else [] + params.append(pytest.param(codec_id, marks=marks, id=codec_id)) + return params + + +@pytest.fixture(autouse=True) +def _allow_pickle(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(pickles, "ALLOW_UNSAFE_PICKLE_DECODE", True) + + +def _is_buffer_like(obj: object) -> bool: + try: + memoryview(obj) # type: ignore[arg-type] + except TypeError: + return False + return True + + +def test_every_registered_codec_has_a_case() -> None: + """A new codec must be added to `CASES` so that the contract is checked for it.""" + assert set(numcodecs.registry.codec_registry) <= set(CASES) + + +@pytest.mark.parametrize("codec_id", _params(DECODE_RETURNS_BUFFER_XFAIL)) +def test_encode_decode_return_buffers(codec_id: str) -> None: + codec, data = _codec(codec_id), CASES[codec_id].data + enc = codec.encode(data) + assert _is_buffer_like(enc), type(enc) + dec = codec.decode(enc) + assert _is_buffer_like(dec), type(dec) + + +@pytest.mark.parametrize("codec_id", _params(OUT_NDARRAY_XFAIL)) +def test_decode_into_ndarray_returns_out(codec_id: str) -> None: + codec, data = _codec(codec_id), CASES[codec_id].data + enc = codec.encode(data) + expected = codec.decode(enc) + out = np.empty_like(data) + assert codec.decode(enc, out=out) is out + if CASES[codec_id].is_object: + assert all(np.array_equal(a, b) for a, b in zip(out, expected, strict=True)) + else: + assert np.asarray(memoryview(expected)).tobytes() == out.tobytes() + + +@pytest.mark.parametrize( + "codec_id", _params(OUT_BYTEARRAY_XFAIL, skip=NO_BYTEARRAY_OUT, objects=False) +) +def test_decode_into_bytearray_returns_out(codec_id: str) -> None: + codec, data = _codec(codec_id), CASES[codec_id].data + enc = codec.encode(data) + expected = codec.decode(enc) + out = bytearray(data.nbytes) + assert codec.decode(enc, out=out) is out + assert bytes(out) == np.asarray(memoryview(expected)).tobytes() + + +@pytest.mark.parametrize("codec_id", _params(set())) +def test_config_round_trip(codec_id: str) -> None: + codec = _codec(codec_id) + config = codec.get_config() + assert config["id"] == codec.codec_id + rebuilt_json = json.loads(json.dumps(config)) + assert numcodecs.get_codec(rebuilt_json) == codec + without_id = {k: v for k, v in config.items() if k != "id"} + assert type(codec).from_config(without_id) == codec diff --git a/tests/typing/README.md b/tests/typing/README.md new file mode 100644 index 000000000..a885a0967 --- /dev/null +++ b/tests/typing/README.md @@ -0,0 +1,25 @@ +# Static type tests + +The files here check numcodecs' type annotations against its runtime behavior. Each test +pairs `typing.assert_type`, which mypy and pyright check, with a runtime assertion, which +pytest checks. An annotation that type checkers accept but the runtime contradicts makes one +of the two fail. + +Run them from the repository root: + +```bash +uv pip install --group typing +pytest tests/typing +mypy --config-file tests/typing/mypy.ini tests/typing +pyright -p tests/typing +``` + +`mypy.ini` and `pyrightconfig.json` run both checkers in strict mode and read numcodecs from +`src/`. Errors inside numcodecs itself are not reported here: the repository's own mypy +configuration in `pyproject.toml` covers those. + +`pyrightconfig.json` turns off pyright's three `reportUnknown*` rules, because most numcodecs +parameters are not annotated yet. Turn them back on once they are. + +When you change an annotation in numcodecs, add or update an `assert_type` here that shows +what callers now get. diff --git a/tests/typing/__init__.py b/tests/typing/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/typing/mypy.ini b/tests/typing/mypy.ini new file mode 100644 index 000000000..26579ad7e --- /dev/null +++ b/tests/typing/mypy.ini @@ -0,0 +1,15 @@ +# Strict mypy configuration for the static type tests in this directory. +# Run from the repository root: `mypy --config-file tests/typing/mypy.ini tests/typing` +[mypy] +strict = true +warn_unreachable = true +enable_error_code = ignore-without-code, redundant-expr, truthy-bool +# Read numcodecs from the source tree. It does not ship a py.typed marker yet, so mypy +# would otherwise refuse to analyze the installed copy. +mypy_path = src + +# Report errors in the tests only. numcodecs itself is checked by the repository's own +# configuration in pyproject.toml. +[mypy-numcodecs.*] +follow_imports = silent +ignore_errors = true diff --git a/tests/typing/pyrightconfig.json b/tests/typing/pyrightconfig.json new file mode 100644 index 000000000..42540aad6 --- /dev/null +++ b/tests/typing/pyrightconfig.json @@ -0,0 +1,14 @@ +{ + "include": [ + "." + ], + "typeCheckingMode": "strict", + "pythonVersion": "3.12", + "extraPaths": [ + "../../src" + ], + "reportMissingTypeStubs": "none", + "reportUnknownVariableType": "none", + "reportUnknownMemberType": "none", + "reportUnknownArgumentType": "none" +} diff --git a/tests/typing/test_compat_types.py b/tests/typing/test_compat_types.py new file mode 100644 index 000000000..f656c4111 --- /dev/null +++ b/tests/typing/test_compat_types.py @@ -0,0 +1,31 @@ +"""Static types of `numcodecs.compat`, checked against runtime behavior. + +Each `assert_type` is verified by mypy and pyright (see `tests/typing/README.md`), and the +`assert isinstance` next to it is verified by pytest. Together they keep the annotations +honest: a claim that type checkers accept but the runtime contradicts fails here. +""" + +from typing import Any, assert_type + +import numpy as np +import numpy.typing as npt + +from numcodecs.compat import ensure_bytes, ensure_contiguous_ndarray, ensure_ndarray + + +def test_ensure_bytes() -> None: + for buf in (b"abc", bytearray(b"abc"), memoryview(b"abc"), np.arange(3, dtype="u1")): + result = assert_type(ensure_bytes(buf), bytes) + assert isinstance(result, bytes) + + +def test_ensure_ndarray() -> None: + for buf in (b"abc", bytearray(b"abc"), memoryview(b"abc"), np.arange(3, dtype="u1")): + result = assert_type(ensure_ndarray(buf), npt.NDArray[Any]) + assert isinstance(result, np.ndarray) + + +def test_ensure_contiguous_ndarray() -> None: + for buf in (b"abc", bytearray(b"abc"), memoryview(b"abc"), np.arange(3, dtype="u1")): + result = assert_type(ensure_contiguous_ndarray(buf), npt.NDArray[Any]) + assert isinstance(result, np.ndarray) From 145994e1e6c902499d87214a9dca0e556a1b6eb4 Mon Sep 17 00:00:00 2001 From: Davis Vann Bennett Date: Sat, 21 Nov 2026 13:50:06 +0100 Subject: [PATCH 2/2] fix: return the caller's out from decode(buf, out=out) in every codec The Codec docstring says decode writes into `out`, and callers expect `out` back. Ten codecs (Zlib, BZ2, LZMA, Base64, Shuffle, ZFPY, PCodec and the three VLen codecs) rebound `out` to a flattened view and returned that, and numcodecs.compat.ndarray_copy did the same for any `out` that was not an ndarray, which every filter and checksum codec inherited. The decoded bytes were already in the caller's buffer; only the returned object changes. JenkinsLookup3.decode now accepts any contiguous writable buffer as `out` and checks its size, and Fletcher32.decode returns a uint8 ndarray instead of a Cython memoryview slice that no type annotation could name. The strict xfails in tests/test_codec_contract.py are removed. Assisted-by: ClaudeCode:claude-opus-5-5 --- docs/release.md | 9 +++++++++ src/numcodecs/base64.py | 2 +- src/numcodecs/bz2.py | 2 +- src/numcodecs/checksum32.py | 7 ++++++- src/numcodecs/compat.py | 10 ++++++++-- src/numcodecs/fletcher32.pyx | 2 +- src/numcodecs/lzma.py | 2 +- src/numcodecs/pcodec.py | 3 +-- src/numcodecs/shuffle.py | 20 ++++++++------------ src/numcodecs/vlen.pyx | 24 ++++++++++++------------ src/numcodecs/zfpy.py | 2 +- src/numcodecs/zlib.py | 2 +- tests/test_codec_contract.py | 32 ++------------------------------ tests/test_jenkins.py | 7 +++++++ 14 files changed, 59 insertions(+), 65 deletions(-) diff --git a/docs/release.md b/docs/release.md index 6847e8ea6..51e2933bb 100644 --- a/docs/release.md +++ b/docs/release.md @@ -38,6 +38,15 @@ * `import numcodecs` no longer fails when a zfpy post-release such as 1.0.1.post0 is installed. zfpy versions are now parsed with `packaging`, which becomes a dependency, and the `zfpy` extra now requires zfpy >= 1.0.1. {issue}`892` +* `decode(buf, out=out)` now returns `out` itself for every codec, as the `Codec` + docstring describes. Zlib, BZ2, LZMA, Base64, Shuffle, ZFPY, PCodec, the three VLen + codecs, and every codec built on `numcodecs.compat.ndarray_copy` returned a flattened view + of `out` instead, or a new ndarray when `out` was a `bytearray`. The decoded bytes were + already written into `out`; only the returned object changes. +* `JenkinsLookup3.decode` accepts any contiguous writable buffer as `out`, not only an + ndarray, and raises `ValueError` when `out` has the wrong size. +* `Fletcher32.decode` without `out` returns a `uint8` ndarray, like the other checksum + codecs, instead of a Cython memoryview slice. (release_0.17.1)= diff --git a/src/numcodecs/base64.py b/src/numcodecs/base64.py index 307c6d52b..c107d5c7b 100644 --- a/src/numcodecs/base64.py +++ b/src/numcodecs/base64.py @@ -19,7 +19,7 @@ def decode(self, buf, out=None): # normalise inputs buf = ensure_contiguous_ndarray(buf) if out is not None: - out = ensure_contiguous_ndarray(out) + ensure_contiguous_ndarray(out) # validate; decode into `out` itself below # do decompression decompressed = _base64.standard_b64decode(buf) # handle destination diff --git a/src/numcodecs/bz2.py b/src/numcodecs/bz2.py index 2b63b147b..a3c8f5ec3 100644 --- a/src/numcodecs/bz2.py +++ b/src/numcodecs/bz2.py @@ -31,7 +31,7 @@ def decode(self, buf, out=None): # normalise inputs buf = ensure_contiguous_ndarray(buf) if out is not None: - out = ensure_contiguous_ndarray(out) + ensure_contiguous_ndarray(out) # validate; decode into `out` itself below # N.B., bz2 cannot handle ndarray directly because of truth testing issues buf = memoryview(buf) diff --git a/src/numcodecs/checksum32.py b/src/numcodecs/checksum32.py index b02c0a6a2..a095b1718 100644 --- a/src/numcodecs/checksum32.py +++ b/src/numcodecs/checksum32.py @@ -185,7 +185,12 @@ def decode(self, buf, out=None): "This could be a sign that the data has been corrupted." ) if out is not None: - out.view("uint8")[:] = b[:-4] + out_view = ensure_contiguous_ndarray(out).view("uint8") + if out_view.nbytes != b.nbytes - 4: + raise ValueError( + f"out has {out_view.nbytes} bytes, but the decoded data has {b.nbytes - 4}" + ) + out_view[:] = b[:-4] return out return memoryview(b[:-4]) diff --git a/src/numcodecs/compat.py b/src/numcodecs/compat.py index d1844e10b..cc4d2d139 100644 --- a/src/numcodecs/compat.py +++ b/src/numcodecs/compat.py @@ -175,12 +175,18 @@ def ensure_text(s, encoding="utf-8"): def ndarray_copy(src, dst) -> NDArrayLike: - """Copy the contents of the array from `src` to `dst`.""" + """Copy the contents of the array from `src` to `dst`, and return `dst`. + + If `dst` is `None`, return `src` unchanged. Otherwise `dst` itself is returned, not + a view of it, so callers that pass a `bytearray` get that `bytearray` back. + """ if dst is None: # no-op return src + out = dst + # ensure ndarray like src = ensure_ndarray_like(src) dst = ensure_ndarray_like(dst) @@ -203,4 +209,4 @@ def ndarray_copy(src, dst) -> NDArrayLike: # copy via numpy np.copyto(dst, src) - return dst + return out diff --git a/src/numcodecs/fletcher32.pyx b/src/numcodecs/fletcher32.pyx index 1ecbb45d6..ab9ce6a8f 100644 --- a/src/numcodecs/fletcher32.pyx +++ b/src/numcodecs/fletcher32.pyx @@ -112,5 +112,5 @@ class Fletcher32(Codec): out_ptr = &out_mv[0] memcpy(out_ptr, b_ptr, b_len - FOOTER_LENGTH) else: - out = b_mv[:-FOOTER_LENGTH] + out = b[:-FOOTER_LENGTH] return out diff --git a/src/numcodecs/lzma.py b/src/numcodecs/lzma.py index 7b6b23487..97da02825 100644 --- a/src/numcodecs/lzma.py +++ b/src/numcodecs/lzma.py @@ -59,7 +59,7 @@ def decode(self, buf, out=None): # normalise inputs buf = ensure_contiguous_ndarray(buf) if out is not None: - out = ensure_contiguous_ndarray(out) + ensure_contiguous_ndarray(out) # validate; decode into `out` itself below # do decompression dec = _lzma.decompress(buf, format=self.format, filters=self.filters) diff --git a/src/numcodecs/pcodec.py b/src/numcodecs/pcodec.py index c7108c7b8..739666b11 100644 --- a/src/numcodecs/pcodec.py +++ b/src/numcodecs/pcodec.py @@ -122,8 +122,7 @@ def encode(self, buf): def decode(self, buf, out=None): buf = ensure_bytes(buf) if out is not None: - out = ensure_contiguous_ndarray(out) - standalone.simple_decompress_into(buf, out) + standalone.simple_decompress_into(buf, ensure_contiguous_ndarray(out)) return out else: return standalone.simple_decompress(buf) diff --git a/src/numcodecs/shuffle.py b/src/numcodecs/shuffle.py index 0e7031f5e..e29e5a593 100644 --- a/src/numcodecs/shuffle.py +++ b/src/numcodecs/shuffle.py @@ -38,24 +38,20 @@ def _prepare_arrays(self, buf, out): return buf, out def encode(self, buf, out=None): - buf, out = self._prepare_arrays(buf, out) + buf, out_arr = self._prepare_arrays(buf, out) - if self.elementsize <= 1: - return out # no shuffling needed - - _doShuffle(buf.view("uint8"), out.view("uint8"), self.elementsize) + if self.elementsize > 1: + _doShuffle(buf.view("uint8"), out_arr.view("uint8"), self.elementsize) - return out + return out_arr if out is None else out def decode(self, buf, out=None): - buf, out = self._prepare_arrays(buf, out) - - if self.elementsize <= 1: - return out # no shuffling needed + buf, out_arr = self._prepare_arrays(buf, out) - _doUnshuffle(buf.view("uint8"), out.view("uint8"), self.elementsize) + if self.elementsize > 1: + _doUnshuffle(buf.view("uint8"), out_arr.view("uint8"), self.elementsize) - return out + return out_arr if out is None else out def __repr__(self): return f'{type(self).__name__}(elementsize={self.elementsize})' diff --git a/src/numcodecs/vlen.pyx b/src/numcodecs/vlen.pyx index f51e8cad7..424cc7ee6 100644 --- a/src/numcodecs/vlen.pyx +++ b/src/numcodecs/vlen.pyx @@ -154,11 +154,11 @@ class VLenUTF8(Codec): # load number of items n_items = load_le32(data) - # setup output + # setup output: fill a flat view, but return the caller's own `out` if out is not None: - out = check_out_param(out, n_items) + out_flat = check_out_param(out, n_items) else: - out = np.empty(n_items, dtype=object) + out = out_flat = np.empty(n_items, dtype=object) # iterate and decode - N.B., do not try to cast `out` as object[:] # as this causes segfaults, possibly similar to @@ -171,7 +171,7 @@ class VLenUTF8(Codec): data += HEADER_LENGTH if data + L > data_end: raise ValueError('corrupt buffer, data seem truncated') - out[i] = PyUnicode_FromStringAndSize(data, L) + out_flat[i] = PyUnicode_FromStringAndSize(data, L) data += L return out @@ -283,11 +283,11 @@ class VLenBytes(Codec): # load number of items n_items = load_le32(data) - # setup output + # setup output: fill a flat view, but return the caller's own `out` if out is not None: - out = check_out_param(out, n_items) + out_flat = check_out_param(out, n_items) else: - out = np.empty(n_items, dtype=object) + out = out_flat = np.empty(n_items, dtype=object) # iterate and decode - N.B., do not try to cast `out` as object[:] # as this causes segfaults, possibly similar to @@ -300,7 +300,7 @@ class VLenBytes(Codec): data += HEADER_LENGTH if data + L > data_end: raise ValueError('corrupt buffer, data seem truncated') - out[i] = PyBytes_FromStringAndSize(data, L) + out_flat[i] = PyBytes_FromStringAndSize(data, L) data += L return out @@ -439,11 +439,11 @@ class VLenArray(Codec): # load number of items n_items = load_le32(data) - # setup output + # setup output: fill a flat view, but return the caller's own `out` if out is not None: - out = check_out_param(out, n_items) + out_flat = check_out_param(out, n_items) else: - out = np.empty(n_items, dtype=object) + out = out_flat = np.empty(n_items, dtype=object) # iterate and decode - N.B., do not try to cast `out` as object[:] # as this causes segfaults, possibly similar to @@ -463,7 +463,7 @@ class VLenArray(Codec): v_pb = PyMemoryView_GET_BUFFER(v_mv) memcpy(v_pb.buf, data, L) - out[i] = v + out_flat[i] = v data += L return out diff --git a/src/numcodecs/zfpy.py b/src/numcodecs/zfpy.py index 41ccfe98c..86fb090a8 100644 --- a/src/numcodecs/zfpy.py +++ b/src/numcodecs/zfpy.py @@ -103,7 +103,7 @@ def decode(self, buf, out=None): # normalise inputs buf = ensure_bytes(buf) if out is not None: - out = ensure_contiguous_ndarray(out) + ensure_contiguous_ndarray(out) # validate; decode into `out` itself below # do decompression dec = _zfpy.decompress_numpy(buf) diff --git a/src/numcodecs/zlib.py b/src/numcodecs/zlib.py index ca247af29..e62658dcb 100644 --- a/src/numcodecs/zlib.py +++ b/src/numcodecs/zlib.py @@ -31,7 +31,7 @@ def decode(self, buf, out=None): # normalise inputs buf = ensure_contiguous_ndarray(buf) if out is not None: - out = ensure_contiguous_ndarray(out) + ensure_contiguous_ndarray(out) # validate; decode into `out` itself below # do decompression dec = _zlib.decompress(buf) diff --git a/tests/test_codec_contract.py b/tests/test_codec_contract.py index c328a25cc..bf47cc007 100644 --- a/tests/test_codec_contract.py +++ b/tests/test_codec_contract.py @@ -85,36 +85,8 @@ def is_object(self) -> bool: # Known violations of the `out` contract. Each entry is a bug; remove it when fixed. # `strict=True` makes a fixed entry fail, so this list cannot go stale. -OUT_NDARRAY_XFAIL: set[str] = { - "zlib", - "bz2", - "lzma", - "base64", - "shuffle", - "zfpy", - "pcodec", - "vlen-utf8", - "vlen-bytes", - "vlen-array", -} -OUT_BYTEARRAY_XFAIL: set[str] = { - "zlib", - "bz2", - "lzma", - "base64", - "shuffle", - "crc32", - "adler32", - "crc32c", - "jenkins_lookup3", - "astype", - "delta", - "quantize", - "fixedscaleoffset", - "bitround", - "packbits", - "zfpy", -} +OUT_NDARRAY_XFAIL: set[str] = set() +OUT_BYTEARRAY_XFAIL: set[str] = set() DECODE_RETURNS_BUFFER_XFAIL: set[str] = set() diff --git a/tests/test_jenkins.py b/tests/test_jenkins.py index 40d972bf7..511c435ca 100644 --- a/tests/test_jenkins.py +++ b/tests/test_jenkins.py @@ -148,3 +148,10 @@ def test_out(): j = JenkinsLookup3() result = j.encode(data) j.decode(result, out=data) + + +def test_out_wrong_size(): + j = JenkinsLookup3() + enc = j.encode(np.arange(8, dtype="uint8")) + with pytest.raises(ValueError, match="out has 4 bytes, but the decoded data has 8"): + j.decode(enc, out=bytearray(4))