Skip to content

Commit

Permalink
fix(py): Fix array/list value serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Dec 20, 2024
1 parent b7b2bcd commit 1aa5619
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions hugr-py/src/hugr/std/collections/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ArrayVal(val.ExtensionValue):
"""Constant value for a statically sized array of elements."""

v: list[val.Value]
ty: tys.Type
ty: Array

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

Check warning on line 76 in hugr-py/src/hugr/std/collections/array.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/std/collections/array.py#L74-L76

Added lines #L74 - L76 were not covered by tests
name, typ=self.ty, val=serial_val, extensions=[EXTENSION.name]
)

def __str__(self) -> str:
return f"array({comma_sep_str(self.v)})"
8 changes: 6 additions & 2 deletions hugr-py/src/hugr/std/collections/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ListVal(val.ExtensionValue):
"""Constant value for a list of elements."""

v: list[val.Value]
ty: tys.Type
ty: List

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

Check warning on line 55 in hugr-py/src/hugr/std/collections/list.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/std/collections/list.py#L53-L55

Added lines #L53 - L55 were not covered by tests
name, typ=self.ty, val=serial_val, extensions=[EXTENSION.name]
)

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

0 comments on commit 1aa5619

Please sign in to comment.