From d01f00db1fef62ef6fb3f3bda89be9b7da52603a Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sat, 1 Mar 2025 11:22:01 +0000 Subject: [PATCH] Make Checksum32 an ABC --- numcodecs/checksum32.py | 31 ++++++++++++++++++++++--------- pyproject.toml | 7 ++----- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/numcodecs/checksum32.py b/numcodecs/checksum32.py index f1d82623..fc6cafd0 100644 --- a/numcodecs/checksum32.py +++ b/numcodecs/checksum32.py @@ -1,11 +1,12 @@ +import abc import struct import zlib -from collections.abc import Callable from contextlib import suppress from types import ModuleType -from typing import TYPE_CHECKING, Literal, Optional +from typing import Literal, Optional import numpy as np +from typing_extensions import Buffer from .abc import Codec from .compat import ensure_contiguous_ndarray, ndarray_copy @@ -15,15 +16,11 @@ with suppress(ImportError): import crc32c as _crc32c # type: ignore[no-redef, unused-ignore] -if TYPE_CHECKING: # pragma: no cover - from typing_extensions import Buffer - CHECKSUM_LOCATION = Literal['start', 'end'] -class Checksum32(Codec): +class Checksum32(Codec, abc.ABC): # override in sub-class - checksum: Callable[["Buffer", int], int] | None = None location: CHECKSUM_LOCATION = 'start' def __init__(self, location: CHECKSUM_LOCATION | None = None): @@ -67,6 +64,10 @@ def decode(self, buf, out=None): ) return ndarray_copy(payload_view, out) + @staticmethod + @abc.abstractmethod + def checksum(data: Buffer, value: int) -> int: ... + class CRC32(Checksum32): """Codec add a crc32 checksum to the buffer. @@ -78,9 +79,15 @@ class CRC32(Checksum32): """ codec_id = 'crc32' - checksum = zlib.crc32 location = 'start' + @staticmethod + def checksum(data: Buffer, value: int = 0) -> int: + """ + Thin wrapper around ``zlib.crc32``. + """ + return zlib.crc32(data, value) + class Adler32(Checksum32): """Codec add a adler32 checksum to the buffer. @@ -92,9 +99,15 @@ class Adler32(Checksum32): """ codec_id = 'adler32' - checksum = zlib.adler32 location = 'start' + @staticmethod + def checksum(data: Buffer, value: int = 1) -> int: + """ + Thin wrapper around ``zlib.adler32``. + """ + return zlib.adler32(data, value) + class JenkinsLookup3(Checksum32): """Bob Jenkin's lookup3 checksum with 32-bit output diff --git a/pyproject.toml b/pyproject.toml index 29ef0bf0..dc96c58b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,11 +13,8 @@ name = "numcodecs" description = """ A Python package providing buffer compression and transformation codecs \ for use in data storage and communication applications.""" -readme = "README.rst" -dependencies = [ - "numpy>=1.24", - "deprecated" -] +readme = "README.rst" +dependencies = ["numpy>=1.24", "deprecated", "typing_extensions"] requires-python = ">=3.11" dynamic = [ "version",