diff --git a/numcodecs/abc.py b/numcodecs/abc.py index 5aba8c25..a57c13e2 100644 --- a/numcodecs/abc.py +++ b/numcodecs/abc.py @@ -29,14 +29,13 @@ """ from abc import ABC, abstractmethod -from typing import Optional +from typing import ClassVar class Codec(ABC): """Codec abstract base class.""" - # override in sub-class - codec_id: Optional[str] = None + codec_id: ClassVar[str] """Codec identifier.""" @abstractmethod diff --git a/numcodecs/gzip.py b/numcodecs/gzip.py index 4269bad9..90b6df10 100644 --- a/numcodecs/gzip.py +++ b/numcodecs/gzip.py @@ -1,5 +1,6 @@ import gzip as _gzip import io +from typing import ClassVar, Literal from .abc import Codec from .compat import ensure_bytes, ensure_contiguous_ndarray @@ -15,12 +16,13 @@ class GZip(Codec): """ - codec_id = 'gzip' + codec_id: ClassVar[Literal['gzip']] = 'gzip' + level: int def __init__(self, level=1): self.level = level - def encode(self, buf): + def encode(self, buf) -> bytes: # normalise inputs buf = ensure_contiguous_ndarray(buf) @@ -31,7 +33,7 @@ def encode(self, buf): return compressed.getvalue() # noinspection PyMethodMayBeStatic - def decode(self, buf, out=None): + def decode(self, buf, out=None) -> bytes: # normalise inputs # BytesIO only copies if the data is not of `bytes` type. # This allows `bytes` objects to pass through without copying. diff --git a/numcodecs/tests/test_registry.py b/numcodecs/tests/test_registry.py index 93ed8f03..b5863eb3 100644 --- a/numcodecs/tests/test_registry.py +++ b/numcodecs/tests/test_registry.py @@ -34,6 +34,7 @@ def test_all_classes_registered(): if ( inspect.isclass(obj) and issubclass(obj, numcodecs.abc.Codec) + and hasattr(obj, 'codec_id') and obj.codec_id not in numcodecs.registry.codec_registry and obj.codec_id is not None # remove `None` )