Skip to content

Commit

Permalink
refactor(hugr-py): common up a null check (#1205)
Browse files Browse the repository at this point in the history
avoids some bugs I kept making because of name similarities
  • Loading branch information
ss2165 authored Jun 18, 2024
1 parent 2bb079f commit 04ed329
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions hugr-py/src/hugr/_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,22 @@ def __call__(self) -> Command:
return super().__call__()


V = TypeVar("V")


def _check_complete(v: V | None) -> V:
if v is None:
raise IncompleteOp()
return v


@dataclass()
class Output(DataflowOp, PartialOp):
_types: tys.TypeRow | None = None

@property
def types(self) -> tys.TypeRow:
if self._types is None:
raise IncompleteOp()
return self._types
return _check_complete(self._types)

def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.Output:
return sops.Output(parent=parent.idx, types=ser_it(self.types))
Expand Down Expand Up @@ -143,9 +150,7 @@ class MakeTupleDef(DataflowOp, PartialOp):

@property
def types(self) -> tys.TypeRow:
if self._types is None:
raise IncompleteOp()
return self._types
return _check_complete(self._types)

def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.MakeTuple:
return sops.MakeTuple(
Expand All @@ -172,9 +177,7 @@ class UnpackTupleDef(DataflowOp, PartialOp):

@property
def types(self) -> tys.TypeRow:
if self._types is None:
raise IncompleteOp()
return self._types
return _check_complete(self._types)

@property
def num_out(self) -> int | None:
Expand Down Expand Up @@ -297,9 +300,7 @@ class DataflowBlock(DfParentOp):

@property
def sum_rows(self) -> list[tys.TypeRow]:
if self._sum_rows is None:
raise IncompleteOp()
return self._sum_rows
return _check_complete(self._sum_rows)

@property
def other_outputs(self) -> tys.TypeRow:
Expand Down Expand Up @@ -348,9 +349,7 @@ class ExitBlock(Op):

@property
def cfg_outputs(self) -> tys.TypeRow:
if self._cfg_outputs is None:
raise IncompleteOp()
return self._cfg_outputs
return _check_complete(self._cfg_outputs)

def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.ExitBlock:
return sops.ExitBlock(
Expand Down

0 comments on commit 04ed329

Please sign in to comment.