Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
29 changes: 29 additions & 0 deletions numcodecs/tests/test_zarr3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import pickle
from typing import TYPE_CHECKING

import numpy as np
Expand Down Expand Up @@ -277,3 +278,31 @@
def test_to_dict():
codec = numcodecs.zarr3.LZ4(level=5)
assert codec.to_dict() == {"name": "numcodecs.lz4", "configuration": {"level": 5}}


@pytest.mark.parametrize(

Check warning on line 283 in numcodecs/tests/test_zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/tests/test_zarr3.py#L283

Added line #L283 was not covered by tests
"codec_cls",
[
numcodecs.zarr3.Blosc,
numcodecs.zarr3.LZ4,
numcodecs.zarr3.Zstd,
numcodecs.zarr3.Zlib,
numcodecs.zarr3.GZip,
numcodecs.zarr3.BZ2,
numcodecs.zarr3.LZMA,
numcodecs.zarr3.Shuffle,
],
)
def test_codecs_pickleable(codec_cls):
codec = codec_cls()

Check warning on line 297 in numcodecs/tests/test_zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/tests/test_zarr3.py#L296-L297

Added lines #L296 - L297 were not covered by tests

expected = codec

Check warning on line 299 in numcodecs/tests/test_zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/tests/test_zarr3.py#L299

Added line #L299 was not covered by tests

p = pickle.dumps(codec)
actual = pickle.loads(p)
assert actual == expected

Check warning on line 303 in numcodecs/tests/test_zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/tests/test_zarr3.py#L301-L303

Added lines #L301 - L303 were not covered by tests

print(codec)
print(codec.codec_name)
print(codec.__doc__)

Check warning on line 307 in numcodecs/tests/test_zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/tests/test_zarr3.py#L305-L307

Added lines #L305 - L307 were not covered by tests
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for numcodecs.zarr3.Blosc this returns

Blosc(codec_name='numcodecs.Blosc', codec_config={})
numcodecs.Blosc

            See :class:`numcodecs.blosc.Blosc` for more details and parameters.
            

which all seems correct to me?

#assert False
62 changes: 40 additions & 22 deletions numcodecs/zarr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from functools import cached_property, partial
from typing import Any, Self, TypeVar
from warnings import warn
import textwrap

import numpy as np

Expand Down Expand Up @@ -79,6 +80,16 @@
codec_name: str
codec_config: dict[str, JSON]

def __init_subclass__(cls, *, namespace: str | None = None, codec_name: str | None = None, **kwargs):

Check warning on line 83 in numcodecs/zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/zarr3.py#L83

Added line #L83 was not covered by tests
"""To be used only when creating the actual public-facing codec class."""
super().__init_subclass__(**kwargs)
if namespace is not None and codec_name is not None:
cls_name = f"{CODEC_PREFIX}{namespace}.{codec_name}"
cls.codec_name = f"{CODEC_PREFIX}{codec_name}"
cls.__doc__ = f"""

Check warning on line 89 in numcodecs/zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/zarr3.py#L85-L89

Added lines #L85 - L89 were not covered by tests
See :class:`{cls_name}` for more details and parameters.
"""

def __init__(self, **codec_config: JSON) -> None:
if not self.codec_name:
raise ValueError(
Expand Down Expand Up @@ -184,30 +195,18 @@


def _add_docstring(cls: type[T], ref_class_name: str) -> type[T]:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this can be deleted once the changes in this PR are applied to all the other codecs

cls.__doc__ = f"""
cls.__doc__ = textwrap.dedent(

Check warning on line 198 in numcodecs/zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/zarr3.py#L198

Added line #L198 was not covered by tests
f"""
See :class:`{ref_class_name}` for more details and parameters.
"""
)
return cls


def _add_docstring_wrapper(ref_class_name: str) -> partial:
return partial(_add_docstring, ref_class_name=ref_class_name)


def _make_bytes_bytes_codec(codec_name: str, cls_name: str) -> type[_NumcodecsBytesBytesCodec]:
# rename for class scope
_codec_name = CODEC_PREFIX + codec_name

class _Codec(_NumcodecsBytesBytesCodec):
codec_name = _codec_name

def __init__(self, **codec_config: JSON) -> None:
super().__init__(**codec_config)

_Codec.__name__ = cls_name
return _Codec


def _make_array_array_codec(codec_name: str, cls_name: str) -> type[_NumcodecsArrayArrayCodec]:
# rename for class scope
_codec_name = CODEC_PREFIX + codec_name
Expand Down Expand Up @@ -254,13 +253,32 @@


# bytes-to-bytes codecs
Blosc = _add_docstring(_make_bytes_bytes_codec("blosc", "Blosc"), "numcodecs.blosc.Blosc")
LZ4 = _add_docstring(_make_bytes_bytes_codec("lz4", "LZ4"), "numcodecs.lz4.LZ4")
Zstd = _add_docstring(_make_bytes_bytes_codec("zstd", "Zstd"), "numcodecs.zstd.Zstd")
Zlib = _add_docstring(_make_bytes_bytes_codec("zlib", "Zlib"), "numcodecs.zlib.Zlib")
GZip = _add_docstring(_make_bytes_bytes_codec("gzip", "GZip"), "numcodecs.gzip.GZip")
BZ2 = _add_docstring(_make_bytes_bytes_codec("bz2", "BZ2"), "numcodecs.bz2.BZ2")
LZMA = _add_docstring(_make_bytes_bytes_codec("lzma", "LZMA"), "numcodecs.lzma.LZMA")
class Blosc(_NumcodecsBytesBytesCodec, namespace="blosc", codec_name="Blosc"):
pass

Check warning on line 257 in numcodecs/zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/zarr3.py#L256-L257

Added lines #L256 - L257 were not covered by tests


class LZ4(_NumcodecsBytesBytesCodec, namespace="lz4", codec_name="LZ4"):
pass

Check warning on line 261 in numcodecs/zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/zarr3.py#L260-L261

Added lines #L260 - L261 were not covered by tests


class Zstd(_NumcodecsBytesBytesCodec, namespace="zstd", codec_name="Zstd"):
pass

Check warning on line 265 in numcodecs/zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/zarr3.py#L264-L265

Added lines #L264 - L265 were not covered by tests


class Zlib(_NumcodecsBytesBytesCodec, namespace="zlib", codec_name="Zlib"):
pass

Check warning on line 269 in numcodecs/zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/zarr3.py#L268-L269

Added lines #L268 - L269 were not covered by tests


class GZip(_NumcodecsBytesBytesCodec, namespace="gzip", codec_name="GZip"):
pass

Check warning on line 273 in numcodecs/zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/zarr3.py#L272-L273

Added lines #L272 - L273 were not covered by tests


class BZ2(_NumcodecsBytesBytesCodec, namespace="bz2", codec_name="BZ2"):
pass

Check warning on line 277 in numcodecs/zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/zarr3.py#L276-L277

Added lines #L276 - L277 were not covered by tests


class LZMA(_NumcodecsBytesBytesCodec, namespace="lzma",codec_name="LZMA"):
pass

Check warning on line 281 in numcodecs/zarr3.py

View check run for this annotation

Codecov / codecov/patch

numcodecs/zarr3.py#L280-L281

Added lines #L280 - L281 were not covered by tests


@_add_docstring_wrapper("numcodecs.shuffle.Shuffle")
Expand Down
Loading