Latest Results
test: Better python roundtrip tests, and lots of fixes (#2436)
The python validation tests did a roundtrip serialization check by
encoding the HUGR, loading it, encoding it again, and checking for
differences in the serialization.
This ignored any information loss in the first encoding, and resulted in
a lot of hidden bugs.
This PR changes the test in `conftest.py` to instead compare the
original HUGR and the loaded one directly, using a "node hash" that
computes the main properties of each node and its children in an
index-independent way. (we do not traverse graph edges to avoid having a
graph isomorphism problem).
The node hash is defined as follows, and should be easily extensible if
needed. We compare hugrs by checking the hashes of their root modules.
```python
@dataclass
class _NodeHash:
op: str
entrypoint: bool
input_neighbours: int
output_neighbours: int
input_ports: int
output_ports: int
input_order_edges: int
output_order_edges: int
is_region: bool
node_depth: int
children_hashes: list[_NodeHash] # sorted
metadata: dict[str, str]
```
This revealed a bunch of bugs with the json serialization, hugr
builders, node iterators, ...:
- **Order edges were serialized incorrectly**. The json encoding uses
`null` for order edges, but python emitted `#ports` instead (and this
caused problems when combined with the out port inconsistencies below).
- Standardize string/repr formatting for specialized types and values,
so e.g. after roundtriping a `Some(TRUE)` it still shows as `Some`
instead of `Sum(tag=1, tys=[[], [Bool]], val=[TRUE])`.
- `Hugr.num_outgoing` and `num_incoming` were counting ports instead of
edges...
- Fixed many inconsistencies with the number of output ports in
`FuncDefn`, `Case`, and the likes.
- The output port count wasn't being set in a lot of cases (this worked
fine in the tests because adding an edge auto-allocates the ports).
- Order edges were inconsistently reported in the neighbours / links
iterators. I fixed it and added new tests.
- Probably more bugfixes I'm missing.
The roundtrip checks also have options for checking all the format
combinations (one variable for the encoding format and another for
converting it with `hugr convert` before loading if necessary).
This only does `json-json` for the moment, as `hugr-model` detects
multiple errors. Active Branches
#24430%
#24420%
#24110%
© 2025 CodSpeed Technology