|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import importlib |
4 | | -import importlib.util |
5 | 3 | import json |
6 | 4 | import re |
7 | 5 | from dataclasses import asdict |
8 | 6 |
|
9 | 7 | import numpy as np |
10 | 8 | import pytest |
11 | 9 | from pydantic import ValidationError |
| 10 | +from typing_extensions import TypedDict |
12 | 11 |
|
13 | 12 | from pydantic_zarr.experimental.core import json_eq |
14 | 13 | from pydantic_zarr.experimental.v3 import ( |
|
23 | 22 | auto_codecs, |
24 | 23 | ) |
25 | 24 |
|
26 | | -from ..conftest import DTYPE_EXAMPLES_V3, DTypeExample |
27 | | - |
28 | | -ZARR_AVAILABLE = importlib.util.find_spec("zarr") is not None |
| 25 | +from ..conftest import DTYPE_EXAMPLES_V3, ZARR_AVAILABLE, DTypeExample |
29 | 26 |
|
30 | 27 |
|
31 | 28 | @pytest.mark.parametrize("invalid_dimension_names", [[], "hi", ["1", 2, None]], ids=str) |
@@ -304,6 +301,50 @@ def test_dim_names_from_zarr_array( |
304 | 301 | assert spec.dimension_names == expected_names |
305 | 302 |
|
306 | 303 |
|
| 304 | +@pytest.mark.skipif(not ZARR_AVAILABLE, reason="zarr-python is not installed") |
| 305 | +def test_typed_members() -> None: |
| 306 | + """ |
| 307 | + Test GroupSpec creation with typed members |
| 308 | + """ |
| 309 | + |
| 310 | + array1d = ArraySpec( |
| 311 | + shape=(1,), |
| 312 | + data_type="uint8", |
| 313 | + chunk_grid={"name": "regular", "configuration": {"chunk_shape": (1,)}}, |
| 314 | + chunk_key_encoding={"name": "default", "configuration": {"separator": "/"}}, |
| 315 | + fill_value=0, |
| 316 | + codecs=({"name": "bytes"},), |
| 317 | + attributes={}, |
| 318 | + ) |
| 319 | + |
| 320 | + class DatasetMembers(TypedDict): |
| 321 | + x: ArraySpec |
| 322 | + y: ArraySpec |
| 323 | + |
| 324 | + class DatasetGroup(GroupSpec): |
| 325 | + members: DatasetMembers |
| 326 | + |
| 327 | + class ExpectedMembers(TypedDict): |
| 328 | + r10m: DatasetGroup |
| 329 | + r20m: DatasetGroup |
| 330 | + |
| 331 | + class ExpectedGroup(GroupSpec): |
| 332 | + members: ExpectedMembers |
| 333 | + |
| 334 | + flat = { |
| 335 | + "": BaseGroupSpec(attributes={}), |
| 336 | + "/r10m": BaseGroupSpec(attributes={}), |
| 337 | + "/r20m": BaseGroupSpec(attributes={}), |
| 338 | + "/r10m/x": array1d, |
| 339 | + "/r10m/y": array1d, |
| 340 | + "/r20m/x": array1d, |
| 341 | + "/r20m/y": array1d, |
| 342 | + } |
| 343 | + |
| 344 | + zg = GroupSpec.from_flat(flat).to_zarr({}, path="") |
| 345 | + ExpectedGroup.from_zarr(zg) |
| 346 | + |
| 347 | + |
307 | 348 | def test_arrayspec_with_methods() -> None: |
308 | 349 | """ |
309 | 350 | Test that ArraySpec with_* methods create new validated copies |
|
0 commit comments