Skip to content

Commit 725cf25

Browse files
authored
Make Checksum32 an ABC (#711)
1 parent 56abf3a commit 725cf25

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

numcodecs/checksum32.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import abc
12
import struct
23
import zlib
3-
from collections.abc import Callable
44
from contextlib import suppress
55
from types import ModuleType
6-
from typing import TYPE_CHECKING, Literal, Optional
6+
from typing import Literal, Optional
77

88
import numpy as np
9+
from typing_extensions import Buffer
910

1011
from .abc import Codec
1112
from .compat import ensure_contiguous_ndarray, ndarray_copy
@@ -15,15 +16,11 @@
1516
with suppress(ImportError):
1617
import crc32c as _crc32c # type: ignore[no-redef, unused-ignore]
1718

18-
if TYPE_CHECKING: # pragma: no cover
19-
from typing_extensions import Buffer
20-
2119
CHECKSUM_LOCATION = Literal['start', 'end']
2220

2321

24-
class Checksum32(Codec):
22+
class Checksum32(Codec, abc.ABC):
2523
# override in sub-class
26-
checksum: Callable[["Buffer", int], int] | None = None
2724
location: CHECKSUM_LOCATION = 'start'
2825

2926
def __init__(self, location: CHECKSUM_LOCATION | None = None):
@@ -67,6 +64,10 @@ def decode(self, buf, out=None):
6764
)
6865
return ndarray_copy(payload_view, out)
6966

67+
@staticmethod
68+
@abc.abstractmethod
69+
def checksum(data: Buffer, value: int) -> int: ...
70+
7071

7172
class CRC32(Checksum32):
7273
"""Codec add a crc32 checksum to the buffer.
@@ -78,9 +79,15 @@ class CRC32(Checksum32):
7879
"""
7980

8081
codec_id = 'crc32'
81-
checksum = zlib.crc32
8282
location = 'start'
8383

84+
@staticmethod
85+
def checksum(data: Buffer, value: int = 0) -> int:
86+
"""
87+
Thin wrapper around ``zlib.crc32``.
88+
"""
89+
return zlib.crc32(data, value)
90+
8491

8592
class Adler32(Checksum32):
8693
"""Codec add a adler32 checksum to the buffer.
@@ -92,9 +99,15 @@ class Adler32(Checksum32):
9299
"""
93100

94101
codec_id = 'adler32'
95-
checksum = zlib.adler32
96102
location = 'start'
97103

104+
@staticmethod
105+
def checksum(data: Buffer, value: int = 1) -> int:
106+
"""
107+
Thin wrapper around ``zlib.adler32``.
108+
"""
109+
return zlib.adler32(data, value)
110+
98111

99112
class JenkinsLookup3(Checksum32):
100113
"""Bob Jenkin's lookup3 checksum with 32-bit output

pyproject.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ name = "numcodecs"
1313
description = """
1414
A Python package providing buffer compression and transformation codecs \
1515
for use in data storage and communication applications."""
16-
readme = "README.rst"
17-
dependencies = [
18-
"numpy>=1.24",
19-
"deprecated"
20-
]
16+
readme = "README.rst"
17+
dependencies = ["numpy>=1.24", "deprecated", "typing_extensions"]
2118
requires-python = ">=3.11"
2219
dynamic = [
2320
"version",

0 commit comments

Comments
 (0)