33from dataclasses import dataclass , replace
44from enum import Enum
55from functools import cached_property
6- from typing import TYPE_CHECKING
6+ from typing import TYPE_CHECKING , cast
77
88import numcodecs
99from numcodecs .blosc import Blosc
1010
11- from zarr .abc .codec import BytesBytesCodec
11+ from zarr .abc .codec import BytesBytesCodec , CodecConfigDict , CodecDict
1212from zarr .core .buffer .cpu import as_numpy_array_wrapper
1313from zarr .core .common import JSON , parse_enum , parse_named_configuration , to_thread
1414from zarr .registry import register_codec
@@ -54,6 +54,22 @@ class BloscCname(Enum):
5454 zlib = "zlib"
5555
5656
57+ class BloscCodecConfigDict (CodecConfigDict ):
58+ """A dictionary representing a Blosc codec configuration."""
59+
60+ typesize : int
61+ cname : BloscCname
62+ clevel : int
63+ shuffle : BloscShuffle
64+ blocksize : int
65+
66+
67+ class BloscCodecDict (CodecDict [BloscCodecConfigDict ]):
68+ """A dictionary representing a Blosc codec."""
69+
70+ ...
71+
72+
5773# See https://zarr.readthedocs.io/en/stable/tutorial.html#configuring-blosc
5874numcodecs .blosc .use_threads = False
5975
@@ -118,12 +134,12 @@ def from_dict(cls, data: dict[str, JSON]) -> Self:
118134 _ , configuration_parsed = parse_named_configuration (data , "blosc" )
119135 return cls (** configuration_parsed ) # type: ignore[arg-type]
120136
121- def to_dict (self ) -> dict [ str , JSON ] :
137+ def to_dict (self ) -> BloscCodecDict :
122138 if self .typesize is None :
123139 raise ValueError ("`typesize` needs to be set for serialization." )
124140 if self .shuffle is None :
125141 raise ValueError ("`shuffle` needs to be set for serialization." )
126- return {
142+ out_dict = {
127143 "name" : "blosc" ,
128144 "configuration" : {
129145 "typesize" : self .typesize ,
@@ -134,6 +150,8 @@ def to_dict(self) -> dict[str, JSON]:
134150 },
135151 }
136152
153+ return cast (BloscCodecDict , out_dict )
154+
137155 def evolve_from_array_spec (self , array_spec : ArraySpec ) -> Self :
138156 dtype = array_spec .dtype
139157 new_codec = self
0 commit comments