|
10 | 10 | from numpy.lib.recfunctions import repack_fields |
11 | 11 |
|
12 | 12 | from zarr.core.common import NamedConfig |
| 13 | +from zarr.core.context import Context, Resolver |
13 | 14 | from zarr.core.dtype.common import ( |
14 | 15 | DTypeConfig_V2, |
15 | 16 | DTypeJSON, |
|
24 | 25 | bytes_to_json, |
25 | 26 | check_json_str, |
26 | 27 | ) |
| 28 | +from zarr.core.dtype.registry import _NestedDataTypeValidationError |
27 | 29 | from zarr.core.dtype.wrapper import TBaseDType, TBaseScalar, ZDType |
28 | 30 | from zarr.errors import DataTypeValidationError, ZarrUserWarning |
29 | 31 |
|
@@ -74,20 +76,22 @@ def _unsupported_field_feature(dtype: np.dtype[np.void]) -> str | None: |
74 | 76 | return None |
75 | 77 |
|
76 | 78 |
|
77 | | -def _field_from_json( |
78 | | - data: DTypeJSON, *, zarr_format: ZarrFormat |
79 | | -) -> ZDType[TBaseDType, TBaseScalar]: |
| 79 | +def _default_resolver(*, zarr_format: ZarrFormat) -> Resolver: |
| 80 | + """A resolver for the default registry, at the top of a document.""" |
| 81 | + return Resolver(context=Context.default(), zarr_format=zarr_format) |
| 82 | + |
| 83 | + |
| 84 | +def _resolve_field(data: DTypeJSON, resolver: Resolver) -> ZDType[TBaseDType, TBaseScalar]: |
80 | 85 | """ |
81 | | - Parse the data type of a structured field, reporting an unrecognized field data type as an |
82 | | - invalid structured data type. |
| 86 | + Resolve the data type of a structured field. A field data type that matches nothing makes the |
| 87 | + structured data type invalid, rather than a different data type. |
83 | 88 | """ |
84 | | - # avoid circular import |
85 | | - from zarr.core.dtype import get_data_type_from_json |
86 | | - |
87 | 89 | try: |
88 | | - return get_data_type_from_json(data, zarr_format=zarr_format) |
| 90 | + return resolver.resolve_data_type(data) |
| 91 | + except DataTypeValidationError: |
| 92 | + raise |
89 | 93 | except ValueError as e: |
90 | | - raise DataTypeValidationError(f"Invalid structured field data type: {e}") from e |
| 94 | + raise _NestedDataTypeValidationError(str(e)) from e |
91 | 95 |
|
92 | 96 |
|
93 | 97 | class StructuredJSON_V2(DTypeConfig_V2[StructuredName_V2, None]): |
@@ -367,32 +371,60 @@ def _check_json_v3(cls, data: DTypeJSON) -> TypeGuard[StructuredJSON_V3]: |
367 | 371 |
|
368 | 372 | @classmethod |
369 | 373 | def _from_json_v2(cls, data: DTypeJSON) -> Self: |
| 374 | + # the fields are resolved from the default registry |
| 375 | + return cls._from_json_resolved_v2(data, resolver=_default_resolver(zarr_format=2)) |
| 376 | + |
| 377 | + @classmethod |
| 378 | + def _from_json_v3(cls, data: DTypeJSON) -> Self: |
| 379 | + # the fields are resolved from the default registry |
| 380 | + return cls._from_json_resolved_v3(data, resolver=_default_resolver(zarr_format=3)) |
| 381 | + |
| 382 | + @classmethod |
| 383 | + def _from_json_resolved(cls, data: DTypeJSON, *, resolver: Resolver) -> Self: |
| 384 | + if resolver.zarr_format == 2: |
| 385 | + return cls._from_json_resolved_v2(data, resolver=resolver) |
| 386 | + if resolver.zarr_format == 3: |
| 387 | + return cls._from_json_resolved_v3(data, resolver=resolver) |
| 388 | + raise ValueError( |
| 389 | + f"zarr_format must be 2 or 3, got {resolver.zarr_format}" |
| 390 | + ) # pragma: no cover |
| 391 | + |
| 392 | + @classmethod |
| 393 | + def _from_json_resolved_v2(cls, data: DTypeJSON, *, resolver: Resolver) -> Self: |
370 | 394 | if cls._check_json_v2(data): |
371 | 395 | # structured dtypes are constructed directly from a list of lists |
372 | 396 | # note that we do not handle the object codec here! this will prevent structured |
373 | 397 | # dtypes from containing object dtypes. |
374 | 398 | name = data["name"] |
375 | 399 | return cls( |
376 | | - fields=tuple( # type: ignore[str-unpack] |
| 400 | + fields=tuple( |
377 | 401 | ( # type: ignore[misc] |
378 | 402 | f_name, |
379 | | - _field_from_json({"name": f_dtype, "object_codec_id": None}, zarr_format=2), |
| 403 | + _resolve_field( |
| 404 | + {"name": f_dtype, "object_codec_id": None}, |
| 405 | + # the location in the data type as written in the document, which |
| 406 | + # is the "name" of the Zarr V2 data type JSON |
| 407 | + resolver.at(index, 1), |
| 408 | + ), |
380 | 409 | ) |
381 | | - for f_name, f_dtype in name |
| 410 | + for index, (f_name, f_dtype) in enumerate(name) |
382 | 411 | ) |
383 | 412 | ) |
384 | 413 | msg = f"Invalid JSON representation of {cls.__name__}. Got {data!r}, expected a JSON array of arrays" |
385 | 414 | raise DataTypeValidationError(msg) |
386 | 415 |
|
387 | 416 | @classmethod |
388 | | - def _from_json_v3(cls, data: DTypeJSON) -> Self: |
| 417 | + def _from_json_resolved_v3(cls, data: DTypeJSON, *, resolver: Resolver) -> Self: |
389 | 418 | if cls._check_json_v3(data): |
390 | 419 | config = data["configuration"] |
391 | 420 | meta_fields = config["fields"] |
392 | 421 | return cls( |
393 | 422 | fields=tuple( |
394 | | - (f_name, _field_from_json(f_dtype, zarr_format=3)) # type: ignore[misc] |
395 | | - for f_name, f_dtype in meta_fields |
| 423 | + ( # type: ignore[misc] |
| 424 | + f_name, |
| 425 | + _resolve_field(f_dtype, resolver.at("configuration", "fields", index, 1)), |
| 426 | + ) |
| 427 | + for index, (f_name, f_dtype) in enumerate(meta_fields) |
396 | 428 | ) |
397 | 429 | ) |
398 | 430 | msg = f"Invalid JSON representation of {cls.__name__}. Got {data!r}, expected a JSON object with the key {cls._zarr_v3_name!r}" |
@@ -645,19 +677,21 @@ def _check_json_v3(cls, data: DTypeJSON) -> TypeGuard[StructJSON_V3]: # type: i |
645 | 677 | ) |
646 | 678 |
|
647 | 679 | @classmethod |
648 | | - def _from_json_v3(cls, data: DTypeJSON) -> Self: |
| 680 | + def _from_json_resolved_v3(cls, data: DTypeJSON, *, resolver: Resolver) -> Self: |
649 | 681 | if cls._check_json_v3(data): |
650 | 682 | config = data["configuration"] |
651 | 683 | meta_fields = config["fields"] |
652 | 684 | parsed_fields: list[tuple[str, ZDType[TBaseDType, TBaseScalar]]] = [] |
653 | | - for field in meta_fields: |
| 685 | + for index, field in enumerate(meta_fields): |
654 | 686 | if isinstance(field, dict): |
655 | 687 | f_name = field["name"] |
656 | 688 | f_dtype = field["data_type"] |
| 689 | + f_resolver = resolver.at("configuration", "fields", index, "data_type") |
657 | 690 | else: |
658 | 691 | # Legacy tuple-style field format from "structured" dtype |
659 | 692 | f_name, f_dtype = field # type: ignore[unreachable] |
660 | | - parsed_fields.append((f_name, _field_from_json(f_dtype, zarr_format=3))) # type: ignore[arg-type] |
| 693 | + f_resolver = resolver.at("configuration", "fields", index, 1) |
| 694 | + parsed_fields.append((f_name, _resolve_field(f_dtype, f_resolver))) # type: ignore[arg-type] |
661 | 695 | return cls(fields=tuple(parsed_fields)) |
662 | 696 | msg = f"Invalid JSON representation of {cls.__name__}. Got {data!r}, expected a JSON object with the key {cls._zarr_v3_name!r}" |
663 | 697 | raise DataTypeValidationError(msg) |
|
0 commit comments