Skip to content

Resolves issue #2881 by removing partial codec initialization #2896

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 1 commit 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
24 changes: 7 additions & 17 deletions src/zarr/codecs/blosc.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,25 @@ def parse_blocksize(data: JSON) -> int:
class BloscCodec(BytesBytesCodec):
is_fixed_size = False

typesize: int | None
typesize: int
cname: BloscCname = BloscCname.zstd
clevel: int = 5
shuffle: BloscShuffle | None = BloscShuffle.noshuffle
shuffle: BloscShuffle
blocksize: int = 0

def __init__(
self,
*,
typesize: int | None = None,
typesize: int,
cname: BloscCname | str = BloscCname.zstd,
clevel: int = 5,
shuffle: BloscShuffle | str | None = None,
shuffle: BloscShuffle | str,
blocksize: int = 0,
) -> None:
typesize_parsed = parse_typesize(typesize) if typesize is not None else None
typesize_parsed = parse_typesize(typesize)
cname_parsed = parse_enum(cname, BloscCname)
clevel_parsed = parse_clevel(clevel)
shuffle_parsed = parse_enum(shuffle, BloscShuffle) if shuffle is not None else None
shuffle_parsed = parse_enum(shuffle, BloscShuffle)
blocksize_parsed = parse_blocksize(blocksize)

object.__setattr__(self, "typesize", typesize_parsed)
Expand All @@ -120,10 +120,6 @@ def from_dict(cls, data: dict[str, JSON]) -> Self:
return cls(**configuration_parsed) # type: ignore[arg-type]

def to_dict(self) -> dict[str, JSON]:
if self.typesize is None:
raise ValueError("`typesize` needs to be set for serialization.")
if self.shuffle is None:
raise ValueError("`shuffle` needs to be set for serialization.")
return {
"name": "blosc",
"configuration": {
Expand All @@ -141,17 +137,11 @@ def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self:
if new_codec.typesize is None:
new_codec = replace(new_codec, typesize=dtype.itemsize)
if new_codec.shuffle is None:
new_codec = replace(
new_codec,
shuffle=(BloscShuffle.bitshuffle if dtype.itemsize == 1 else BloscShuffle.shuffle),
)

new_codec = replace(new_codec,shuffle=(BloscShuffle.bitshuffle if dtype.itemsize == 1 else BloscShuffle.shuffle),)
return new_codec

@cached_property
def _blosc_codec(self) -> Blosc:
if self.shuffle is None:
raise ValueError("`shuffle` needs to be set for decoding and encoding.")
map_shuffle_str_to_int = {
BloscShuffle.noshuffle: 0,
BloscShuffle.shuffle: 1,
Expand Down
15 changes: 5 additions & 10 deletions src/zarr/codecs/bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,22 @@ class Endian(Enum):
class BytesCodec(ArrayBytesCodec):
is_fixed_size = True

endian: Endian | None
endian: Endian

def __init__(self, *, endian: Endian | str | None = default_system_endian) -> None:
endian_parsed = None if endian is None else parse_enum(endian, Endian)
def __init__(self, *, endian: Endian | str = default_system_endian) -> None:
endian_parsed = parse_enum(endian, Endian)

object.__setattr__(self, "endian", endian_parsed)

@classmethod
def from_dict(cls, data: dict[str, JSON]) -> Self:
_, configuration_parsed = parse_named_configuration(
data, "bytes", require_configuration=False
data, "bytes", require_configuration=True
)
configuration_parsed = configuration_parsed or {}
return cls(**configuration_parsed) # type: ignore[arg-type]

def to_dict(self) -> dict[str, JSON]:
if self.endian is None:
return {"name": "bytes"}
else:
return {"name": "bytes", "configuration": {"endian": self.endian.value}}
return {"name": "bytes", "configuration": {"endian": self.endian.value}}

def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self:
if array_spec.dtype.itemsize == 0:
Expand Down Expand Up @@ -104,7 +100,6 @@ async def _encode_single(
assert isinstance(chunk_array, NDBuffer)
if (
chunk_array.dtype.itemsize > 1
and self.endian is not None
and self.endian != chunk_array.byteorder
):
# type-ignore is a numpy bug
Expand Down
Loading