Skip to content

Commit

Permalink
test ops strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Aug 29, 2024
1 parent 4e484c7 commit 79f9b00
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
8 changes: 6 additions & 2 deletions hugr-py/src/hugr/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import hugr._serialization.ops as sops
from hugr import tys, val
from hugr.hugr.node_port import Direction, InPort, Node, OutPort, PortOffset, Wire
from hugr.utils import ser_it
from hugr.utils import comma_sep_str, ser_it

if TYPE_CHECKING:
from collections.abc import Sequence
Expand Down Expand Up @@ -297,7 +297,11 @@ def num_out(self) -> int:
return len(self.outer_signature().output)

def name(self) -> str:
return self.ext_op._op_def.qualified_name()
name = self.ext_op._op_def.qualified_name()
ta = self.type_args()
if len(ta) == 0:
return name
return f"{name}<{comma_sep_str(self.type_args())}>"


@dataclass(frozen=True, eq=False)
Expand Down
10 changes: 10 additions & 0 deletions hugr-py/src/hugr/tys.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,16 @@ def resolve(self, registry: ext.ExtensionRegistry) -> PolyFuncType:
def __str__(self) -> str:
return f"∀ {comma_sep_str(self.params)}. {self.body!s}"

@classmethod
def empty(cls) -> PolyFuncType:
"""Generate an empty polymorphic function type.
Example:
>>> PolyFuncType.empty()
PolyFuncType(params=[], body=FunctionType([], []))
"""
return PolyFuncType(params=[], body=FunctionType.empty())


@dataclass
class ExtType(Type):
Expand Down
66 changes: 66 additions & 0 deletions hugr-py/tests/test_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import pytest

from hugr.ops import (
CFG,
DFG,
AliasDecl,
AliasDefn,
Call,
CallIndirect,
Case,
Conditional,
Const,
DataflowBlock,
ExitBlock,
FuncDecl,
FuncDefn,
Input,
LoadConst,
LoadFunc,
MakeTuple,
Module,
Noop,
Op,
Output,
Tag,
TailLoop,
UnpackTuple,
)
from hugr.std.int import DivMod
from hugr.std.logic import Not
from hugr.tys import Bool, PolyFuncType, TypeBound
from hugr.val import TRUE


@pytest.mark.parametrize(
("op", "string"),
[
(Input([]), "Input"),
(Output([]), "Output"),
(Not, "logic.Not"),
(DivMod, "arithmetic.int.idivmod_u<5>"),
(MakeTuple(), "MakeTuple"),
(UnpackTuple(), "UnpackTuple"),
(Tag(0, Bool), "Tag(0)"),
(CFG([]), "CFG"),
(DFG([]), "DFG"),
(DataflowBlock([]), "DataflowBlock"),
(ExitBlock([]), "ExitBlock"),
(LoadConst(), "LoadConst"),
(Conditional(Bool, []), "Conditional"),
(TailLoop([], []), "TailLoop"),
(Case([]), "Case"),
(Module(), "Module"),
(Call(PolyFuncType.empty()), "Call"),
(CallIndirect(), "CallIndirect"),
(LoadFunc(PolyFuncType.empty()), "LoadFunc"),
(FuncDefn("foo", []), "FuncDefn(foo)"),
(FuncDecl("bar", PolyFuncType.empty()), "FuncDecl(bar)"),
(Const(TRUE), "Const(TRUE)"),
(Noop(), "Noop"),
(AliasDecl("baz", TypeBound.Any), "AliasDecl(baz)"),
(AliasDefn("baz", Bool), "AliasDefn(baz)"),
],
)
def test_ops_str(op: Op, string: str):
assert op.name() == string

0 comments on commit 79f9b00

Please sign in to comment.