Skip to content

Commit 7bf85b9

Browse files
authored
fix(py): Fix array/list value serialization (#1827)
The rust-side serialization definition for custom constants is a bit more complex than what we had written on the python side for array and list values. This fixes #1826, and was tested by running the issue's example from guppy. - > Why aren't we testing this locally? Constant value deserialization is a "best effort" thing. If we can match the serialized thing with one of the hardcoded implementors of `CustomConst` (registered in a global var by `typetag`) then we deserialize into the specific class. If we cannot deserialize into any specific class, we use `CustomSerialized` by default. So even if we build a constant from python and `validate` using the rust checker, the deserialization will succeed and validate correctly. It'd be nice to fail the deserialization if a constant claims to be of some specific kind but cannot be deserialized into it. I'll open an issue and look a bit into it.
1 parent b7b2bcd commit 7bf85b9

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

hugr-py/src/hugr/std/collections/array.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ArrayVal(val.ExtensionValue):
6060
"""Constant value for a statically sized array of elements."""
6161

6262
v: list[val.Value]
63-
ty: tys.Type
63+
ty: Array
6464

6565
def __init__(self, v: list[val.Value], elem_ty: tys.Type) -> None:
6666
self.v = v
@@ -71,7 +71,11 @@ def to_value(self) -> val.Extension:
7171
# The value list must be serialized at this point, otherwise the
7272
# `Extension` value would not be serializable.
7373
vs = [v._to_serial_root() for v in self.v]
74-
return val.Extension(name, typ=self.ty, val=vs, extensions=[EXTENSION.name])
74+
element_ty = self.ty.ty._to_serial_root()
75+
serial_val = {"values": vs, "typ": element_ty}
76+
return val.Extension(
77+
name, typ=self.ty, val=serial_val, extensions=[EXTENSION.name]
78+
)
7579

7680
def __str__(self) -> str:
7781
return f"array({comma_sep_str(self.v)})"

hugr-py/src/hugr/std/collections/list.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ListVal(val.ExtensionValue):
3939
"""Constant value for a list of elements."""
4040

4141
v: list[val.Value]
42-
ty: tys.Type
42+
ty: List
4343

4444
def __init__(self, v: list[val.Value], elem_ty: tys.Type) -> None:
4545
self.v = v
@@ -50,7 +50,11 @@ def to_value(self) -> val.Extension:
5050
# The value list must be serialized at this point, otherwise the
5151
# `Extension` value would not be serializable.
5252
vs = [v._to_serial_root() for v in self.v]
53-
return val.Extension(name, typ=self.ty, val=vs, extensions=[EXTENSION.name])
53+
element_ty = self.ty.ty._to_serial_root()
54+
serial_val = {"values": vs, "typ": element_ty}
55+
return val.Extension(
56+
name, typ=self.ty, val=serial_val, extensions=[EXTENSION.name]
57+
)
5458

5559
def __str__(self) -> str:
5660
return f"[{comma_sep_str(self.v)}]"

0 commit comments

Comments
 (0)