44literals like `zarr_format`), not domain validity. Each concept gets a
55`validate_*` function returning every problem found, an `is_*` type guard,
66and a `parse_*` function that narrows or raises `MetadataValidationError`.
7+ The guards are `TypeGuard`s, not `TypeIs`: True narrows a value to its
8+ document type, and False says nothing about its type, since a value can be
9+ well typed and still not a valid document.
710
811Every `ValidationProblem` carries a machine-readable `kind` alongside its
912human-readable `message`, so consumers can dispatch on the failure mode
1720import math
1821from collections .abc import Mapping , Sequence
1922from dataclasses import dataclass
20- from typing import Final , Literal , cast
21-
22- from typing_extensions import TypeIs
23+ from typing import Final , Literal , TypeGuard , cast
2324
2425from zarr_metadata ._common import JSONValue
2526from zarr_metadata .v2 .array import ZarrV2ArrayMetadataJSON
@@ -160,7 +161,7 @@ def _refine(value: object, loc: tuple[str | int, ...], *, finite: bool) -> _Refi
160161 )
161162
162163
163- def _is_canonical_json (value : object , * , finite : bool = True ) -> TypeIs [JSONValue ]:
164+ def _is_canonical_json (value : object , * , finite : bool = True ) -> TypeGuard [JSONValue ]:
164165 """Whether `value` already uses the concrete containers in `JSONValue`.
165166
166167 A non-finite number counts only when `finite` is false, as a document's
@@ -182,7 +183,7 @@ def _is_canonical_json(value: object, *, finite: bool = True) -> TypeIs[JSONValu
182183 return False
183184
184185
185- def is_json (value : object ) -> TypeIs [JSONValue ]:
186+ def is_json (value : object ) -> TypeGuard [JSONValue ]:
186187 """Whether `value` is a canonical JSON structure (recursively)."""
187188 return _is_canonical_json (value )
188189
@@ -362,7 +363,7 @@ def validate_metadata_field_v3(
362363 return tuple (problems )
363364
364365
365- def is_metadata_field_v3 (value : object ) -> TypeIs [ZarrV3MetadataFieldJSON ]:
366+ def is_metadata_field_v3 (value : object ) -> TypeGuard [ZarrV3MetadataFieldJSON ]:
366367 """Whether `value` is a v3 metadata field: a bare name or a named config."""
367368 if isinstance (value , str ):
368369 return True
@@ -621,7 +622,7 @@ def validate_array_metadata_v3(value: object) -> tuple[ValidationProblem, ...]:
621622 return tuple (problems )
622623
623624
624- def is_array_metadata_v3 (value : object ) -> TypeIs [ZarrV3ArrayMetadataJSON ]:
625+ def is_array_metadata_v3 (value : object ) -> TypeGuard [ZarrV3ArrayMetadataJSON ]:
625626 """Whether `value` is a structurally-valid v3 array metadata document."""
626627 return (
627628 _is_canonical_json (value , finite = False )
@@ -729,7 +730,7 @@ def validate_array_metadata_v2(value: object) -> tuple[ValidationProblem, ...]:
729730 return tuple (problems )
730731
731732
732- def is_array_metadata_v2 (value : object ) -> TypeIs [ZarrV2ArrayMetadataJSON ]:
733+ def is_array_metadata_v2 (value : object ) -> TypeGuard [ZarrV2ArrayMetadataJSON ]:
733734 """Whether `value` is a structurally-valid v2 array metadata document."""
734735 return (
735736 _is_canonical_json (value , finite = False )
@@ -842,7 +843,7 @@ def validate_group_metadata_v3(value: object) -> tuple[ValidationProblem, ...]:
842843 return tuple (problems )
843844
844845
845- def is_group_metadata_v3 (value : object ) -> TypeIs [ZarrV3GroupMetadataJSON ]:
846+ def is_group_metadata_v3 (value : object ) -> TypeGuard [ZarrV3GroupMetadataJSON ]:
846847 """Whether `value` is a structurally-valid v3 group metadata document."""
847848 return _is_canonical_json (value , finite = False ) and not validate_group_metadata_v3 (value )
848849
@@ -875,7 +876,7 @@ def validate_group_metadata_v2(value: object) -> tuple[ValidationProblem, ...]:
875876 return tuple (problems )
876877
877878
878- def is_group_metadata_v2 (value : object ) -> TypeIs [ZarrV2GroupMetadataJSON ]:
879+ def is_group_metadata_v2 (value : object ) -> TypeGuard [ZarrV2GroupMetadataJSON ]:
879880 """Whether `value` is a structurally-valid v2 group metadata document."""
880881 return _is_canonical_json (value , finite = False ) and not validate_group_metadata_v2 (value )
881882
0 commit comments