Skip to content

Commit d79b738

Browse files
authored
test(py): check roundtrip through CLI (#2332)
1 parent a1a70f1 commit d79b738

File tree

7 files changed

+24
-12
lines changed

7 files changed

+24
-12
lines changed

hugr-py/src/hugr/_serialization/ops.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,13 @@ class FunctionValue(BaseValue):
123123
"""A higher-order function value."""
124124

125125
v: Literal["Function"] = Field(default="Function", title="ValueTag")
126-
hugr: Any
126+
hugr: str
127127

128128
def deserialize(self) -> val.Value:
129-
from hugr._serialization.serial_hugr import SerialHugr
130129
from hugr.hugr import Hugr
131130

132131
# pydantic stores the serialized dictionary because of the "Any" annotation
133-
return val.Function(Hugr._from_serial(SerialHugr(**self.hugr)))
132+
return val.Function(Hugr.from_str(self.hugr))
134133

135134

136135
class TupleValue(BaseValue):

hugr-py/src/hugr/val.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,7 @@ def type_(self) -> tys.FunctionType:
298298
return self.body.entrypoint_op().inner_signature()
299299

300300
def _to_serial(self) -> sops.FunctionValue:
301-
return sops.FunctionValue(
302-
hugr=self.body._to_serial(),
303-
)
301+
return sops.FunctionValue(hugr=self.body.to_str())
304302

305303
def to_model(self) -> model.Term:
306304
module = self.body.to_model()

hugr-py/tests/conftest.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def validate(
156156
return
157157

158158
# Roundtrip checks
159+
# 1. just roundtrip with Python
159160
if isinstance(h, Hugr):
160161
starting_json = h.to_str()
161162
h2 = Hugr.from_str(starting_json)
@@ -174,14 +175,24 @@ def validate(
174175
roundtrip_encoded = loaded.to_str(EnvelopeConfig.TEXT)
175176
assert encoded == roundtrip_encoded
176177

178+
# 2. roundtrip through the CLI
177179

178-
def _run_hugr_cmd(serial: bytes, cmd: list[str]):
180+
# TODO once model loading is supported in Python
181+
# try every combo of input and output formats
182+
cmd = [*_base_command(), "convert", "-", "--text"]
183+
184+
serial = h.to_bytes(EnvelopeConfig.BINARY)
185+
out = _run_hugr_cmd(serial, cmd)
186+
loaded = Package.from_bytes(out.stdout)
187+
188+
189+
def _run_hugr_cmd(serial: bytes, cmd: list[str]) -> subprocess.CompletedProcess[bytes]:
179190
"""Run a HUGR command.
180191
181192
The `serial` argument is the serialized HUGR to pass to the command via stdin.
182193
"""
183194
try:
184-
subprocess.run(cmd, check=True, input=serial, capture_output=True) # noqa: S603
195+
return subprocess.run(cmd, check=True, input=serial, capture_output=True) # noqa: S603
185196
except subprocess.CalledProcessError as e:
186197
error = e.stderr.decode()
187198
raise RuntimeError(error) from e

specification/schema/hugr_schema_live.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,8 @@
759759
"type": "string"
760760
},
761761
"hugr": {
762-
"title": "Hugr"
762+
"title": "Hugr",
763+
"type": "string"
763764
}
764765
},
765766
"required": [

specification/schema/hugr_schema_strict_live.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,8 @@
759759
"type": "string"
760760
},
761761
"hugr": {
762-
"title": "Hugr"
762+
"title": "Hugr",
763+
"type": "string"
763764
}
764765
},
765766
"required": [

specification/schema/testing_hugr_schema_live.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,8 @@
759759
"type": "string"
760760
},
761761
"hugr": {
762-
"title": "Hugr"
762+
"title": "Hugr",
763+
"type": "string"
763764
}
764765
},
765766
"required": [

specification/schema/testing_hugr_schema_strict_live.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,8 @@
759759
"type": "string"
760760
},
761761
"hugr": {
762-
"title": "Hugr"
762+
"title": "Hugr",
763+
"type": "string"
763764
}
764765
},
765766
"required": [

0 commit comments

Comments
 (0)