Skip to content

Commit 5dd6513

Browse files
committed
Implement struct subclass instead of modifying structured
1 parent 6534763 commit 5dd6513

File tree

5 files changed

+295
-183
lines changed

5 files changed

+295
-183
lines changed

changes/3781.feature.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
Updated structured dtype implementation to match the merged zarr-extensions spec for `struct` data types.
2-
3-
Key changes:
4-
- The primary V3 name is now `struct` (previously `structured`)
5-
- Fields use object format: `{"name": "x", "data_type": "int32"}` instead of tuples
6-
- Fill values use dict format: `{"x": 1, "y": 2.0}` instead of base64
7-
- The `bytes` codec requires explicit `endian` for structured types with multi-byte fields
8-
- Legacy `structured` name with tuple format is accepted for backward compatibility when reading
1+
Added `Struct` class (subclass of `Structured`) implementing the zarr-extensions `struct` dtype spec. Uses object-style field format and dict fill values. Legacy `Structured` remains available for backward compatibility.

src/zarr/core/dtype/__init__.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@
2121
from zarr.core.dtype.npy.complex import Complex64, Complex128
2222
from zarr.core.dtype.npy.float import Float16, Float32, Float64
2323
from zarr.core.dtype.npy.int import Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64
24-
from zarr.core.dtype.npy.structured import Structured, StructuredJSON_V2, StructuredJSON_V3
24+
from zarr.core.dtype.npy.structured import (
25+
Struct,
26+
StructJSON_V3,
27+
Structured,
28+
StructuredJSON_V2,
29+
StructuredJSON_V3,
30+
)
2531
from zarr.core.dtype.npy.time import (
2632
DateTime64,
2733
DateTime64JSON_V2,
@@ -75,6 +81,8 @@
7581
"RawBytes",
7682
"RawBytesJSON_V2",
7783
"RawBytesJSON_V3",
84+
"Struct",
85+
"StructJSON_V3",
7886
"Structured",
7987
"StructuredJSON_V2",
8088
"StructuredJSON_V3",
@@ -125,6 +133,7 @@
125133
| StringDType
126134
| BytesDType
127135
| Structured
136+
| Struct
128137
| TimeDType
129138
| VariableLengthBytes
130139
)
@@ -137,7 +146,7 @@
137146
*COMPLEX_FLOAT_DTYPE,
138147
*STRING_DTYPE,
139148
*BYTES_DTYPE,
140-
Structured,
149+
Struct,
141150
*TIME_DTYPE,
142151
VariableLengthBytes,
143152
)
@@ -155,6 +164,10 @@
155164
# mypy does not know that all the elements of ANY_DTYPE are subclasses of ZDType
156165
data_type_registry.register(dtype._zarr_v3_name, dtype) # type: ignore[arg-type]
157166

167+
# Register Structured for reading legacy "structured" format JSON, but don't include it in
168+
# ANY_DTYPE since it doesn't support native dtype matching (use Struct instead).
169+
data_type_registry.register(Structured._zarr_v3_name, Structured)
170+
158171

159172
# TODO: find a better name for this function
160173
def get_data_type_from_native_dtype(dtype: npt.DTypeLike) -> ZDType[TBaseDType, TBaseScalar]:
@@ -268,7 +281,7 @@ def parse_dtype(
268281
# First attempt to interpret the input as JSON
269282
if isinstance(dtype_spec, Mapping | str | Sequence):
270283
try:
271-
return get_data_type_from_json(dtype_spec, zarr_format=zarr_format) # type: ignore[arg-type]
284+
return get_data_type_from_json(dtype_spec, zarr_format=zarr_format)
272285
except ValueError:
273286
# no data type matched this JSON-like input
274287
pass

0 commit comments

Comments
 (0)