Skip to content

Commit dcd42aa

Browse files
committed
refactor(dtype): return the v2 spellings to match instead of an optional alias
_v2_spellings returns the spellings of a v2 data type JSON in the order they are matched (as written, then canonical), so match_json has no None to check and the precedence rule lives in one place. Assisted-by: ClaudeCode:claude-opus-5-5
1 parent 0d7d5d2 commit dcd42aa

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

‎src/zarr/core/dtype/registry.py‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,20 @@ def _v2_canonical_name(name: str) -> str | None:
5454
return None
5555

5656

57-
def _v2_canonical_alias(data: DTypeJSON) -> DTypeJSON | None:
57+
def _v2_spellings(data: DTypeJSON) -> tuple[DTypeJSON, ...]:
5858
"""
59-
Return the Zarr V2 data type JSON with its name spelled canonically, or None if the name has no
60-
other spelling.
59+
The spellings of a Zarr V2 data type JSON to match, in order: the name as written, then its
60+
canonical spelling if it has another. A data type that declares a non-canonical spelling
61+
(e.g. `">S1"`) takes precedence over the canonical alias, because the name as written is
62+
tried first.
6163
"""
6264
if (
6365
isinstance(data, dict)
6466
and isinstance(name := data.get("name"), str)
6567
and (canonical := _v2_canonical_name(name)) is not None
6668
):
67-
return {**data, "name": canonical}
68-
return None
69+
return (data, {**data, "name": canonical})
70+
return (data,)
6971

7072

7173
# This class is different from the other registry classes, which inherit from
@@ -254,11 +256,7 @@ def match_json(
254256
If no matching Zarr data type is found for the given JSON data.
255257
"""
256258

257-
# A data type that declares a non-canonical spelling takes precedence over the canonical
258-
# alias, so the alias is only tried after the name as written fails to match.
259-
candidates = [data]
260-
if zarr_format == 2 and (alias := _v2_canonical_alias(data)) is not None:
261-
candidates.append(alias)
259+
candidates = _v2_spellings(data) if zarr_format == 2 else (data,)
262260
for candidate in candidates:
263261
for val in self.contents.values():
264262
try:

0 commit comments

Comments
 (0)