Skip to content
Merged
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
34 changes: 21 additions & 13 deletions hugr-py/src/hugr/model/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,7 @@

inputs = [self.link_name(InPort(node, i)) for i in range(node_data._num_inps)]
outputs = [self.link_name(OutPort(node, i)) for i in range(node_data._num_outs)]
meta = []

# Export JSON metadata
for meta_name, meta_value in node_data.metadata.items():
# TODO: Is this the correct way to convert the metadata as JSON?
meta_json = json.dumps(meta_value)
meta.append(
model.Apply(
"compat.meta_json",
[model.Literal(meta_name), model.Literal(meta_json)],
)
)
meta = self.export_json_meta(node)

Check warning on line 65 in hugr-py/src/hugr/model/export.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/model/export.py#L65

Added line #L65 was not covered by tests

# Add an order hint key to the node if necessary
if _needs_order_key(self.hugr, node):
Expand Down Expand Up @@ -374,9 +363,27 @@
error = f"Unknown operation: {op}"
raise ValueError(error)

def export_json_meta(self, node: Node) -> list[model.Term]:
"""Export the metadata of the node via the JSON compatibility constructor."""
node_data = self.hugr[node]
meta: list[model.Term] = []

Check warning on line 369 in hugr-py/src/hugr/model/export.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/model/export.py#L368-L369

Added lines #L368 - L369 were not covered by tests

for meta_name, meta_value in node_data.metadata.items():

Check warning on line 371 in hugr-py/src/hugr/model/export.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/model/export.py#L371

Added line #L371 was not covered by tests
# TODO: Is this the correct way to convert the metadata as JSON?
meta_json = json.dumps(meta_value)
meta.append(

Check warning on line 374 in hugr-py/src/hugr/model/export.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/model/export.py#L373-L374

Added lines #L373 - L374 were not covered by tests
model.Apply(
"compat.meta_json",
[model.Literal(meta_name), model.Literal(meta_json)],
)
)

return meta

Check warning on line 381 in hugr-py/src/hugr/model/export.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/model/export.py#L381

Added line #L381 was not covered by tests

def export_region_module(self, node: Node) -> model.Region:
"""Export a module node as a module region."""
node_data = self.hugr[node]
meta = self.export_json_meta(node)

Check warning on line 386 in hugr-py/src/hugr/model/export.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/model/export.py#L386

Added line #L386 was not covered by tests
children = []

for child in node_data.children:
Expand All @@ -385,7 +392,7 @@
if child_node is not None:
children.append(child_node)

return model.Region(kind=model.RegionKind.MODULE, children=children)
return model.Region(kind=model.RegionKind.MODULE, children=children, meta=meta)

Check warning on line 395 in hugr-py/src/hugr/model/export.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/model/export.py#L395

Added line #L395 was not covered by tests

def export_region_dfg(self, node: Node) -> model.Region:
"""Export the children of a node as a dataflow region."""
Expand Down Expand Up @@ -440,6 +447,7 @@
children=children,
sources=sources,
targets=targets,
meta=meta,
)

def export_region_cfg(self, node: Node) -> model.Region:
Expand Down
Loading