Skip to content

(chore) Type hints for GZip #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions numcodecs/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions numcodecs/gzip.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions numcodecs/tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)
Expand Down
Loading