22import pytest
33import zarr
44from zarr .errors import ContainsGroupError
5- from typing import Any , Literal , TypedDict
5+ from typing import Any , Literal , TypedDict , Union
66import numcodecs
77from pydantic_zarr .core import ArraySpec , GroupSpec , to_zarr , from_zarr
88import numpy as np
@@ -150,12 +150,14 @@ class SubGroupAttrs(TypedDict):
150150 a : str
151151 b : float
152152
153+ SubGroup = GroupSpec [SubGroupAttrs , Any ]
154+
153155 class ArrayAttrs (TypedDict ):
154156 scale : list [float ]
155157
156158 store = zarr .MemoryStore ()
157159
158- spec = GroupSpec (
160+ spec = GroupSpec [ RootAttrs , Union [ ArraySpec , SubGroup ]] (
159161 attrs = RootAttrs (foo = 10 , bar = [0 , 1 , 2 ]),
160162 members = {
161163 "s0" : ArraySpec (
@@ -178,7 +180,7 @@ class ArrayAttrs(TypedDict):
178180 dimension_separator = dimension_separator ,
179181 attrs = ArrayAttrs (scale = [2.0 ]),
180182 ),
181- "subgroup" : GroupSpec (attrs = SubGroupAttrs (a = "foo" , b = 1.0 )),
183+ "subgroup" : SubGroup (attrs = SubGroupAttrs (a = "foo" , b = 1.0 )),
182184 },
183185 )
184186 # materialize a zarr group, based on the spec
@@ -195,6 +197,11 @@ class ArrayAttrs(TypedDict):
195197 group2 = to_zarr (spec , store , "/group_a" , overwrite = True )
196198 assert group2 == group
197199
200+ # again with class methods
201+ group3 = spec .to_zarr (store , "/group_b" )
202+ observed = spec .from_zarr (group3 )
203+ assert observed == spec
204+
198205
199206def test_shape_chunks ():
200207 for a , b in zip (range (1 , 5 ), range (2 , 6 )):
0 commit comments