Skip to content
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
27 changes: 22 additions & 5 deletions hugr-py/src/hugr/hugr/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing_extensions import assert_never

from hugr.hugr import Hugr
from hugr.ops import AsExtOp
from hugr.ops import AsExtOp, Case
from hugr.tys import CFKind, ConstKind, FunctionKind, Kind, OrderKind, ValueKind

from .node_port import InPort, Node, OutPort
Expand Down Expand Up @@ -243,8 +243,22 @@ def _in_order_name(self, n: Node) -> str:
def _out_order_name(self, n: Node) -> str:
return f"{n.idx}:{self._OUTPUT_PREFIX}None"

def _viz_node(self, node: Node, hugr: Hugr, graph: Digraph) -> None:
"""Render a (possibly nested) node to a graphviz graph."""
def _viz_node(
self,
node: Node,
hugr: Hugr,
graph: Digraph,
*,
sibling_order: int | None = None,
) -> None:
"""Render a (possibly nested) node to a graphviz graph.

Args:
node: The node to render.
hugr: The HUGR to render.
graph: The graphviz graph to render the node to.
sibling_order: The order of the node in the region's sibling list.
"""
meta = hugr[node].metadata
if len(meta) > 0 and self.config.display_metadata:
if self.config.max_metadata_length is not None:
Expand Down Expand Up @@ -278,6 +292,9 @@ def _viz_node(self, node: Node, hugr: Hugr, graph: Digraph) -> None:
op = hugr[node].op
if isinstance(op, AsExtOp) and not self.config.qualify_op_name:
op_name = op.op_def().name
elif isinstance(op, Case) and sibling_order is not None:
# Indicate the case number
op_name = f"{op.name()}[{sibling_order}]"
else:
op_name = op.name()

Expand Down Expand Up @@ -311,8 +328,8 @@ def _viz_node(self, node: Node, hugr: Hugr, graph: Digraph) -> None:

if hugr.children(node):
with graph.subgraph(name=f"cluster{node.idx}") as sub:
for child in hugr.children(node):
self._viz_node(child, hugr, sub)
for sibling_order, child in enumerate(hugr.children(node)):
self._viz_node(child, hugr, sub, sibling_order=sibling_order)
html_label = self._format_html_label(**label_config)
sub.node(f"{node.idx}", shape="plain", label=f"<{html_label}>")
sub.attr(
Expand Down
14 changes: 12 additions & 2 deletions hugr-py/src/hugr/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,18 @@ def __repr__(self) -> str:
def __str__(self) -> str:
if len(self.sum_ty.variant_rows) == 2:
left, right = self.sum_ty.variant_rows
if len(left) == 0 and self.tag == 1:
return "Some"
if not left and not right:
# Boolean
if self.tag == 1:
return "True"
else:
return "False"
elif not left:
# Option
if self.tag == 1:
return "Some"
else:
return "None"
elif self.tag == 0:
return "Left"
else:
Expand Down
4 changes: 2 additions & 2 deletions hugr-py/tests/__snapshots__/test_hugr_build.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -2593,7 +2593,7 @@
<TD>
<TABLE BORDER="0" CELLBORDER="0">
<TR><TD><FONT POINT-SIZE="11.0" FACE="monospace"
COLOR="black"><B>Case</B></FONT></TD></TR>
COLOR="black"><B>Case[0]</B></FONT></TD></TR>
</TABLE>
</TD>
</TR>
Expand Down Expand Up @@ -2661,7 +2661,7 @@
<TD>
<TABLE BORDER="0" CELLBORDER="0">
<TR><TD><FONT POINT-SIZE="11.0" FACE="monospace"
COLOR="black"><B>Case</B></FONT></TD></TR>
COLOR="black"><B>Case[1]</B></FONT></TD></TR>
</TABLE>
</TD>
</TR>
Expand Down
5 changes: 4 additions & 1 deletion hugr-py/tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
(DivMod, "arithmetic.int.idivmod_u<5>"),
(MakeTuple(), "MakeTuple"),
(UnpackTuple(), "UnpackTuple"),
(Tag(0, Bool), "Left"),
(Tag(0, Bool), "False"),
(Tag(1, Bool), "True"),
(Tag(0, tys.Option(Bool)), "None"),
(Tag(1, tys.Option(Bool)), "Some"),
(Tag(0, tys.Sum([[Bool, Bool, Bool]])), "Tag(0)"),
(CFG([]), "CFG"),
(DFG([]), "DFG"),
Expand Down
9 changes: 4 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ members = ["hugr-py"]

dev-dependencies = [
"pre-commit >=3.6.2,<4",
"pytest >=8.1.1,<9",
"pytest-cov >=5.0.0,<6",
"pytest ~=9.0",
"pytest-cov ~=7.0",
"pytest-xdist ~= 3.8",
"maturin >=1.7.0,<2",
"mypy >=1.9.0,<2",
"ruff >=0.6.2,<0.7",
"toml >=0.10.0,<0.11",
"syrupy >=4.7.1,<5",
"syrupy ~=5.1",
"types-zstd >= 1.5.6.6",
"pytket >= 1.34.0",
]
Expand All @@ -22,8 +23,6 @@ addopts = "--doctest-modules --ignore=test_files"
filterwarnings = "ignore::DeprecationWarning:lark.*"




[tool.mypy]
# TODO: Fix lints and enable this
#strict = true
Expand Down
48 changes: 36 additions & 12 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading