|
13 | 13 | Self,
|
14 | 14 | TypeAlias,
|
15 | 15 | TypeVar,
|
16 |
| - Union, |
17 | 16 | cast,
|
18 | 17 | overload,
|
19 | 18 | )
|
|
45 | 44 |
|
46 | 45 |
|
47 | 46 | TBaseAttr: TypeAlias = Mapping[str, object] | BaseModel
|
48 |
| -TBaseItem: TypeAlias = Union["GroupSpec", "ArraySpec"] |
| 47 | +TBaseItem: TypeAlias = "GroupSpec[Any, Any] | ArraySpec[Any]" |
49 | 48 |
|
50 | 49 | # These types are for convenience when dealing with unknown ArraySpecs and GroupSpecs
|
51 | 50 | # because type variables don't have default values
|
52 |
| -AnyArraySpec: TypeAlias = "ArraySpec[TBaseAttr]" |
53 |
| -AnyGroupSpec: TypeAlias = "GroupSpec[TBaseAttr, TBaseItem]" |
| 51 | +AnyArraySpec: TypeAlias = "ArraySpec[Any]" |
| 52 | +AnyGroupSpec: TypeAlias = "GroupSpec[Any, Any]" |
| 53 | +AnyNodeSpec: TypeAlias = "AnyArraySpec | AnyGroupSpec" |
54 | 54 |
|
55 | 55 | TAttr = TypeVar("TAttr", bound=TBaseAttr)
|
56 | 56 | TItem = TypeVar("TItem", bound=TBaseItem)
|
@@ -438,7 +438,7 @@ class GroupSpec(NodeSpec, Generic[TAttr, TItem]):
|
438 | 438 | members: Annotated[Mapping[str, TItem] | None, AfterValidator(ensure_key_no_path)] = {}
|
439 | 439 |
|
440 | 440 | @classmethod
|
441 |
| - def from_flat(cls, data: Mapping[str, AnyArraySpec | AnyGroupSpec]) -> Self: |
| 441 | + def from_flat(cls, data: Mapping[str, AnyNodeSpec]) -> Self: |
442 | 442 | """
|
443 | 443 | Create a `GroupSpec` from a flat hierarchy representation.
|
444 | 444 |
|
@@ -490,7 +490,7 @@ def from_flat(cls, data: Mapping[str, AnyArraySpec | AnyGroupSpec]) -> Self:
|
490 | 490 | from_flated = from_flat_group(data)
|
491 | 491 | return cls(**from_flated.model_dump())
|
492 | 492 |
|
493 |
| - def to_flat(self, root_path: str = "") -> dict[str, AnyArraySpec | AnyGroupSpec]: |
| 493 | + def to_flat(self, root_path: str = "") -> dict[str, AnyNodeSpec]: |
494 | 494 | """
|
495 | 495 | Flatten this `GroupSpec`.
|
496 | 496 | This method returns a `dict` with string keys and values that are `GroupSpec` or
|
@@ -527,7 +527,7 @@ def to_flat(self, root_path: str = "") -> dict[str, AnyArraySpec | AnyGroupSpec]
|
527 | 527 | return to_flat(self, root_path=root_path)
|
528 | 528 |
|
529 | 529 | @classmethod
|
530 |
| - def from_zarr(cls, group: zarr.Group, *, depth: int = -1) -> GroupSpec[TAttr, TItem]: |
| 530 | + def from_zarr(cls, group: zarr.Group, *, depth: int = -1) -> AnyGroupSpec: |
531 | 531 | """
|
532 | 532 | Create a GroupSpec from a zarr group. Subgroups and arrays contained in the zarr
|
533 | 533 | group will be converted to instances of GroupSpec and ArraySpec, respectively,
|
@@ -712,7 +712,7 @@ def from_zarr(element: zarr.Array, *, depth: int = ...) -> AnyArraySpec: ...
|
712 | 712 | def from_zarr(element: zarr.Group, *, depth: int = ...) -> AnyGroupSpec: ...
|
713 | 713 |
|
714 | 714 |
|
715 |
| -def from_zarr(element: zarr.Array | zarr.Group, *, depth: int = -1) -> AnyArraySpec | AnyGroupSpec: |
| 715 | +def from_zarr(element: zarr.Array | zarr.Group, *, depth: int = -1) -> AnyNodeSpec: |
716 | 716 | """
|
717 | 717 | Recursively parse a Zarr group or Zarr array into an ArraySpec or GroupSpec.
|
718 | 718 |
|
@@ -755,7 +755,7 @@ def to_zarr(
|
755 | 755 |
|
756 | 756 |
|
757 | 757 | def to_zarr(
|
758 |
| - spec: AnyArraySpec | AnyGroupSpec, |
| 758 | + spec: AnyNodeSpec, |
759 | 759 | store: Store,
|
760 | 760 | path: str,
|
761 | 761 | overwrite: bool = False,
|
@@ -787,7 +787,7 @@ def to_zarr(
|
787 | 787 |
|
788 | 788 |
|
789 | 789 | def from_flat(
|
790 |
| - data: Mapping[str, AnyArraySpec | AnyGroupSpec], |
| 790 | + data: Mapping[str, AnyNodeSpec], |
791 | 791 | ) -> AnyArraySpec | AnyGroupSpec:
|
792 | 792 | """
|
793 | 793 | Wraps `from_flat_group`, handling the special case where a Zarr array is defined at the root of
|
@@ -830,7 +830,7 @@ def from_flat(
|
830 | 830 |
|
831 | 831 |
|
832 | 832 | def from_flat_group(
|
833 |
| - data: Mapping[str, AnyArraySpec | AnyGroupSpec], |
| 833 | + data: Mapping[str, AnyNodeSpec], |
834 | 834 | ) -> AnyGroupSpec:
|
835 | 835 | """
|
836 | 836 | Generate a `GroupSpec` from a flat representation of a hierarchy, i.e. a `dict` with
|
@@ -962,7 +962,7 @@ def auto_dimension_names(data: object) -> tuple[str | None, ...] | None:
|
962 | 962 | return None
|
963 | 963 |
|
964 | 964 |
|
965 |
| -def to_flat(node: ArraySpec | GroupSpec, root_path: str = "") -> dict[str, ArraySpec | GroupSpec]: |
| 965 | +def to_flat(node: AnyNodeSpec, root_path: str = "") -> dict[str, AnyNodeSpec]: |
966 | 966 | """
|
967 | 967 | Flatten a `GroupSpec` or `ArraySpec`.
|
968 | 968 | Converts a `GroupSpec` or `ArraySpec` and a string, into a `dict` with string keys and
|
@@ -1002,7 +1002,7 @@ def to_flat(node: ArraySpec | GroupSpec, root_path: str = "") -> dict[str, Array
|
1002 | 1002 | {'/g1': GroupSpec(zarr_format=3, attributes={'foo': 'bar'}, members=None), '': GroupSpec(zarr_format=2, attributes={'foo': 'bar'}, members=None)}
|
1003 | 1003 | """
|
1004 | 1004 | result = {}
|
1005 |
| - model_copy: AnyArraySpec | AnyGroupSpec |
| 1005 | + model_copy: AnyNodeSpec |
1006 | 1006 | if isinstance(node, ArraySpec):
|
1007 | 1007 | model_copy = node.model_copy(deep=True)
|
1008 | 1008 | else:
|
|
0 commit comments