Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(py): Fix array/list value serialization #1827

Merged
merged 1 commit into from
Dec 20, 2024
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
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 @@
"""Constant value for a statically sized array of elements."""

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is non-breaking because Array satisfies the Type protocol, correct?

Copy link
Collaborator Author

@aborgna-q aborgna-q Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine for reading since it's a type refinement.

For writing into the variable it is actually a breaking change. I assumed the dataclass was frozen, but it seems it's not.

Anyways I'd say mutating the attribute directly is not common (since the type is normally constructed using its custom __init__), and putting in a non-array type should be a bug anyways.


def __init__(self, v: list[val.Value], elem_ty: tys.Type) -> None:
self.v = v
Expand All @@ -71,7 +71,11 @@
# 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 @@
"""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 @@
# 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)}]"
Loading