Skip to content

Commit 3b6705e

Browse files
committed
test(dtype): check v2 JSON alias spellings through the shared wrapper tests
The one-off unitless-time test becomes json_alias_v2_params on the shared dtype test base: (alias, canonical) pairs asserting that reading either spelling gives the same dtype and writing gives the canonical one. The time types list the unitless generic spellings and NumPy's microsecond alias; other types can list their aliases the same way. Assisted-by: ClaudeCode:claude-fable-5
1 parent 7541009 commit 3b6705e

2 files changed

Lines changed: 45 additions & 18 deletions

File tree

‎tests/test_dtype/test_npy/test_time.py‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@ class TestDateTime64(_TestTimeBase):
8080
np.dtype("timedelta64[ns]"),
8181
)
8282
valid_json_v2 = _v2_names("M8")
83+
# The legacy unitless spelling reads as the generic unit and writes back
84+
# bracketed, as the v2 spec requires (#4580); NumPy's microsecond alias
85+
# reads as NumPy's own spelling.
86+
json_alias_v2_params = (
87+
(
88+
{"name": "<M8", "object_codec_id": None},
89+
{"name": "<M8[generic]", "object_codec_id": None},
90+
),
91+
(
92+
{"name": ">M8", "object_codec_id": None},
93+
{"name": ">M8[generic]", "object_codec_id": None},
94+
),
95+
(
96+
{"name": "<M8[μs]", "object_codec_id": None},
97+
{"name": "<M8[us]", "object_codec_id": None},
98+
),
99+
)
83100
valid_json_v3 = _v3_configurations("numpy.datetime64")
84101
invalid_json_v2 = (
85102
{"name": "datetime64", "object_codec_id": None},
@@ -124,6 +141,21 @@ class TestTimeDelta64(_TestTimeBase):
124141
)
125142

126143
valid_json_v2 = _v2_names("m8")
144+
# See TestDateTime64.json_alias_v2_params.
145+
json_alias_v2_params = (
146+
(
147+
{"name": "<m8", "object_codec_id": None},
148+
{"name": "<m8[generic]", "object_codec_id": None},
149+
),
150+
(
151+
{"name": ">m8", "object_codec_id": None},
152+
{"name": ">m8[generic]", "object_codec_id": None},
153+
),
154+
(
155+
{"name": "<m8[μs]", "object_codec_id": None},
156+
{"name": "<m8[us]", "object_codec_id": None},
157+
),
158+
)
127159
valid_json_v3 = _v3_configurations("numpy.timedelta64")
128160
invalid_json_v2 = (
129161
{"name": "timedelta64", "object_codec_id": None},
@@ -279,21 +311,3 @@ def test_generic_time_array_roundtrip(
279311
np.testing.assert_array_equal(
280312
values[4:].view(values.dtype.byteorder + "i8"), [expected_fill, expected_fill]
281313
)
282-
283-
284-
def test_unitless_time_v2_reads_as_generic_and_writes_bracketed() -> None:
285-
"""The legacy unitless spellings stay readable as the generic unit, and write
286-
back with the bracketed unit the v2 spec requires (#4580)."""
287-
cases: tuple[tuple[str, DateTime64 | TimeDelta64], ...] = (
288-
("<M8", DateTime64(unit="generic", scale_factor=1, endianness="little")),
289-
(">M8", DateTime64(unit="generic", scale_factor=1, endianness="big")),
290-
("<m8", TimeDelta64(unit="generic", scale_factor=1, endianness="little")),
291-
(">m8", TimeDelta64(unit="generic", scale_factor=1, endianness="big")),
292-
)
293-
for name, expected in cases:
294-
zdtype = type(expected).from_json({"name": name, "object_codec_id": None}, zarr_format=2)
295-
assert zdtype == expected
296-
assert dict(zdtype.to_json(zarr_format=2)) == {
297-
"name": f"{name}[generic]",
298-
"object_codec_id": None,
299-
}

‎tests/test_dtype/test_wrapper.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ class BaseTestZDType:
9090
# pairs. the first element of the pair is used to create a dtype instance, and the second
9191
# element is the json serialization of the scalar that we want to round-trip.
9292

93+
# for testing v2 JSON spellings that read as a valid dtype but are not the
94+
# spelling zarr writes: (alias json, canonical json) pairs. Reading either
95+
# spelling gives the same dtype, and writing it gives the canonical spelling.
96+
json_alias_v2_params: ClassVar[tuple[tuple[DTypeSpec_V2, DTypeSpec_V2], ...]] = ()
97+
9398
scalar_v2_params: ClassVar[tuple[tuple[ZDType[Any, Any], Any], ...]] = ()
9499
scalar_v3_params: ClassVar[tuple[tuple[Any, Any], ...]] = ()
95100
cast_value_params: ClassVar[tuple[tuple[ZDType[Any, Any], Any, Any], ...]] = ()
@@ -127,6 +132,14 @@ def test_from_json_roundtrip_v3(self, valid_json_v3: DTypeSpec_V3) -> None:
127132
zdtype = self.test_cls.from_json(valid_json_v3, zarr_format=3)
128133
assert zdtype.to_json(zarr_format=3) == valid_json_v3
129134

135+
def test_from_json_alias_v2(
136+
self, json_alias_v2_params: tuple[DTypeSpec_V2, DTypeSpec_V2]
137+
) -> None:
138+
alias, canonical = json_alias_v2_params
139+
zdtype = self.test_cls.from_json(alias, zarr_format=2)
140+
assert zdtype == self.test_cls.from_json(canonical, zarr_format=2)
141+
assert zdtype.to_json(zarr_format=2) == canonical
142+
130143
def test_from_json_invalid_v2(self, invalid_json_v2: DTypeSpec_V2) -> None:
131144
with pytest.raises(DataTypeValidationError):
132145
self.test_cls.from_json(invalid_json_v2, zarr_format=2)

0 commit comments

Comments
 (0)