11from __future__ import annotations
22
3+ import base64
34from collections .abc import Iterable
45from enum import Enum
5- from typing import TYPE_CHECKING
6+ from typing import TYPE_CHECKING , cast
67
78if TYPE_CHECKING :
89 from typing import Any , Literal , Self
@@ -31,7 +32,7 @@ class ArrayV2Metadata(ArrayMetadata):
3132 shape : ChunkCoords
3233 chunk_grid : RegularChunkGrid
3334 data_type : np .dtype [Any ]
34- fill_value : None | int | float = 0
35+ fill_value : None | int | float | str | bytes = 0
3536 order : Literal ["C" , "F" ] = "C"
3637 filters : tuple [numcodecs .abc .Codec , ...] | None = None
3738 dimension_separator : Literal ["." , "/" ] = "."
@@ -140,6 +141,13 @@ def from_dict(cls, data: dict[str, Any]) -> ArrayV2Metadata:
140141 _data = data .copy ()
141142 # check that the zarr_format attribute is correct
142143 _ = parse_zarr_format (_data .pop ("zarr_format" ))
144+ dtype = parse_dtype (_data ["dtype" ])
145+
146+ if dtype .kind in "SV" :
147+ fill_value_encoded = _data .get ("fill_value" )
148+ if fill_value_encoded is not None :
149+ fill_value = base64 .standard_b64decode (fill_value_encoded )
150+ _data ["fill_value" ] = fill_value
143151
144152 # zarr v2 allowed arbitrary keys here.
145153 # We don't want the ArrayV2Metadata constructor to fail just because someone put an
@@ -155,6 +163,14 @@ def from_dict(cls, data: dict[str, Any]) -> ArrayV2Metadata:
155163
156164 def to_dict (self ) -> dict [str , JSON ]:
157165 zarray_dict = super ().to_dict ()
166+
167+ if self .dtype .kind in "SV" and self .fill_value is not None :
168+ # There's a relationship between self.dtype and self.fill_value
169+ # that mypy isn't aware of. The fact that we have S or V dtype here
170+ # means we should have a bytes-type fill_value.
171+ fill_value = base64 .standard_b64encode (cast (bytes , self .fill_value )).decode ("ascii" )
172+ zarray_dict ["fill_value" ] = fill_value
173+
158174 _ = zarray_dict .pop ("chunk_grid" )
159175 zarray_dict ["chunks" ] = self .chunk_grid .chunk_shape
160176
0 commit comments