Skip to content

Commit 7541009

Browse files
committed
refactor(dtype): share the v2 time typestr spelling between the two time types
The generic-unit bracketing moves to TimeDTypeBase._v2_name, which both to_json implementations call, instead of being duplicated in each. Assisted-by: ClaudeCode:claude-fable-5
1 parent 2f9045c commit 7541009

1 file changed

Lines changed: 12 additions & 14 deletions

File tree

‎src/zarr/core/dtype/npy/time.py‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ def __post_init__(self) -> None:
239239
# Store the NumPy spelling; "μs" stays accepted as input and in stored metadata.
240240
object.__setattr__(self, "unit", "us")
241241

242+
def _v2_name(self) -> str:
243+
"""The v2 typestr: NumPy's spelling, except that the generic unit, which
244+
NumPy's `dtype.str` omits, is written in brackets with its scale, because
245+
the v2 spec requires a bracketed unit."""
246+
name: str = self.to_native_dtype().str
247+
if self.unit != "generic":
248+
return name
249+
scale = "" if self.scale_factor == 1 else self.scale_factor
250+
return f"{name}[{scale}generic]"
251+
242252
@classmethod
243253
def from_native_dtype(cls, dtype: TBaseDType) -> Self:
244254
"""
@@ -497,13 +507,7 @@ def to_json(self, zarr_format: ZarrFormat) -> TimeDelta64JSON_V2 | TimeDelta64JS
497507
If the zarr_format is not 2 or 3.
498508
"""
499509
if zarr_format == 2:
500-
name: str = self.to_native_dtype().str
501-
if self.unit == "generic":
502-
# NumPy's dtype.str omits the generic unit (and its scale), but the
503-
# v2 spec requires a bracketed unit, so write it explicitly.
504-
scale = "" if self.scale_factor == 1 else self.scale_factor
505-
name += f"[{scale}generic]"
506-
return {"name": name, "object_codec_id": None}
510+
return {"name": self._v2_name(), "object_codec_id": None}
507511
elif zarr_format == 3:
508512
return {
509513
"name": self._zarr_v3_name,
@@ -787,13 +791,7 @@ def to_json(self, zarr_format: ZarrFormat) -> DateTime64JSON_V2 | DateTime64JSON
787791
If the zarr_format is not 2 or 3.
788792
"""
789793
if zarr_format == 2:
790-
name: str = self.to_native_dtype().str
791-
if self.unit == "generic":
792-
# NumPy's dtype.str omits the generic unit (and its scale), but the
793-
# v2 spec requires a bracketed unit, so write it explicitly.
794-
scale = "" if self.scale_factor == 1 else self.scale_factor
795-
name += f"[{scale}generic]"
796-
return {"name": name, "object_codec_id": None}
794+
return {"name": self._v2_name(), "object_codec_id": None}
797795
elif zarr_format == 3:
798796
return {
799797
"name": self._zarr_v3_name,

0 commit comments

Comments
 (0)