Skip to content

Commit

Permalink
example data-type extension
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmoore committed Feb 18, 2025
1 parent 24ef221 commit ace0846
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/zarr/core/metadata/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ class DataType(Enum):
complex128 = "complex128"
string = "string"
bytes = "bytes"
example = "https://example.com"

@property
def byte_count(self) -> int | None:
Expand All @@ -630,6 +631,7 @@ def byte_count(self) -> int | None:
DataType.float64: 8,
DataType.complex64: 8,
DataType.complex128: 16,
DataType.example: 32,
}
try:
return data_type_byte_counts[self]
Expand Down Expand Up @@ -657,9 +659,11 @@ def to_numpy_shortname(self) -> str:
DataType.float64: "f8",
DataType.complex64: "c8",
DataType.complex128: "c16",
DataType.example: np.dtype(object),
}
return data_type_to_numpy[self]


def to_numpy(self) -> np.dtypes.StringDType | np.dtypes.ObjectDType | np.dtype[Any]:
# note: it is not possible to round trip DataType <-> np.dtype
# due to the fact that DataType.string and DataType.bytes both
Expand Down
15 changes: 15 additions & 0 deletions tests/test_metadata/test_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,18 @@ def test_dtypes(dtype_str: str) -> None:
else:
# return type for vlen types may vary depending on numpy version
assert dt.byte_count is None

async def test_ext_uri() -> None:
metadata_dict = {
"zarr_format": 3,
"node_type": "array",
"shape": (1,),
"chunk_grid": {"name": "regular", "configuration": {"chunk_shape": (1,)}},
"data_type": "https://example.com",
"chunk_key_encoding": {"name": "default", "separator": "."},
"codecs": [{"name": "bytes"}],
"fill_value": 0,
}
m = ArrayV3Metadata.from_dict(metadata_dict)
d = json.loads(m.to_buffer_dict(default_buffer_prototype())["zarr.json"].to_bytes())
assert m.data_type is DataType.example

0 comments on commit ace0846

Please sign in to comment.