From 36548ab4e377ca9074a80355fabd51693d89c649 Mon Sep 17 00:00:00 2001 From: Seyon Sivarajah Date: Fri, 11 Oct 2024 17:16:29 +0200 Subject: [PATCH] feat: `instantiate` method for `OpDef` (#1576) Closes #1512 drive-by: typos --- hugr-py/src/hugr/ext.py | 14 ++++++++++++++ hugr-py/src/hugr/ops.py | 6 +++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/hugr-py/src/hugr/ext.py b/hugr-py/src/hugr/ext.py index 3910b7ae7..533e55cd7 100644 --- a/hugr-py/src/hugr/ext.py +++ b/hugr-py/src/hugr/ext.py @@ -218,6 +218,20 @@ def qualified_name(self) -> str: return f"{ext_name}.{self.name}" return self.name + def instantiate( + self, + args: Sequence[tys.TypeArg] | None = None, + concrete_signature: tys.FunctionType | None = None, + ) -> ops.ExtOp: + """Instantiate an operation from this definition. + + Args: + args: Type arguments corresponding to the type parameters of the definition. + concrete_signature: Concrete function type of the operation, only required + if the operation is polymorphic. + """ + return ops.ExtOp(self, concrete_signature, list(args or [])) + @dataclass class ExtensionValue(ExtensionObject): diff --git a/hugr-py/src/hugr/ops.py b/hugr-py/src/hugr/ops.py index 738898a75..a97470adf 100644 --- a/hugr-py/src/hugr/ops.py +++ b/hugr-py/src/hugr/ops.py @@ -230,7 +230,7 @@ def ext_op(self) -> ExtOp: Computed once using :meth:`op_def` :meth:`type_args` and :meth:`type_args`. Each of those methods should be deterministic. """ - return ExtOp(self.op_def(), self.cached_signature(), self.type_args()) + return self.op_def().instantiate(self.type_args(), self.cached_signature()) def op_def(self) -> ext.OpDef: """The :class:`tys.OpDef` for this operation. @@ -657,7 +657,7 @@ def name(self) -> str: class DataflowBlock(DfParentOp): """Parent of non-entry basic block in a control flow graph.""" - #: Inputs types of the innner dataflow graph. + #: Inputs types of the inner dataflow graph. inputs: tys.TypeRow _sum: tys.Sum | None = None _other_outputs: tys.TypeRow | None = field(default=None, repr=False) @@ -879,7 +879,7 @@ def name(self) -> str: class Case(DfParentOp): """Parent of a dataflow graph that is a branch of a :class:`Conditional`.""" - #: Inputs types of the innner dataflow graph. + #: Inputs types of the inner dataflow graph. inputs: tys.TypeRow _outputs: tys.TypeRow | None = field(default=None, repr=False) num_out: int = field(default=0, repr=False)