Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/pydantic_zarr/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,10 +983,14 @@ def from_flat_group(data: dict[str, AnyArraySpec | AnyGroupSpec]) -> AnyGroupSpe
# this is an array or group that belongs to the group we are ultimately returning
if isinstance(value, ArraySpec):
member_arrays[subparent_name] = value
else:
elif isinstance(value, GroupSpec):
if subparent_name not in submember_by_parent_name:
submember_by_parent_name[subparent_name] = {}
submember_by_parent_name[subparent_name][root_name] = value
else:
raise ValueError(
f"Value at '{key}' is not a v2 ArraySpec or GroupSpec (got {type(value)=})"
)
else:
# these are groups or arrays that belong to one of the member groups
# not great that we repeat this conditional dict initialization
Expand Down
6 changes: 5 additions & 1 deletion src/pydantic_zarr/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,10 +968,14 @@ def from_flat_group(
# this is an array or group that belongs to the group we are ultimately returning
if isinstance(value, ArraySpec):
member_arrays[subparent_name] = value
else:
elif isinstance(value, GroupSpec):
if subparent_name not in submember_by_parent_name:
submember_by_parent_name[subparent_name] = {}
submember_by_parent_name[subparent_name][root_name] = value
else:
raise ValueError(
f"Value at '{key}' is not a v3 ArraySpec or GroupSpec (got {type(value)=})"
)
else:
# these are groups or arrays that belong to one of the member groups
# not great that we repeat this conditional dict initialization
Expand Down
14 changes: 14 additions & 0 deletions tests/test_pydantic_zarr/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

import json
import re
from typing import TYPE_CHECKING, Any

import pytest
Expand Down Expand Up @@ -654,3 +655,16 @@ def test_arrayspec_from_zarr(dtype_example: DTypeExample) -> None:
observed["dtype"] = list(observed["dtype"])

assert arr_spec.model_dump() == observed


def test_mix_v3_v2_fails() -> None:
from pydantic_zarr.v3 import ArraySpec as ArraySpecv3

members_flat = {"/a": ArraySpecv3.from_array(np.ones(1))}
with pytest.raises(
ValueError,
match=re.escape(
"Value at '/a' is not a v2 ArraySpec or GroupSpec (got type(value)=<class 'pydantic_zarr.v3.ArraySpec'>)"
),
):
GroupSpec.from_flat(members_flat) # type: ignore[arg-type]
14 changes: 14 additions & 0 deletions tests/test_pydantic_zarr/test_v3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
import re
from dataclasses import asdict

import numpy as np
Expand Down Expand Up @@ -236,3 +237,16 @@ def test_from_zarr_depth() -> None:
assert group_in_3.attributes == tree[""].attributes # type: ignore[attr-defined]
assert group_in_3.members["1"].attributes == tree["/1"].attributes # type: ignore[attr-defined]
assert group_in_3.members["1"].members["2"].attributes == tree["/1/2"].attributes # type: ignore[attr-defined]


def test_mix_v3_v2_fails() -> None:
from pydantic_zarr.v2 import ArraySpec as ArraySpecv2

members_flat = {"/a": ArraySpecv2.from_array(np.ones(1))}
with pytest.raises(
ValueError,
match=re.escape(
"Value at '/a' is not a v3 ArraySpec or GroupSpec (got type(value)=<class 'pydantic_zarr.v2.ArraySpec'>)"
),
):
GroupSpec.from_flat(members_flat) # type: ignore[arg-type]
Loading