Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(hugr-py): common up a null check #1205

Merged
merged 1 commit into from
Jun 18, 2024
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
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 @@
return super().__call__()


V = TypeVar("V")


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

Check warning on line 98 in hugr-py/src/hugr/_ops.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/_ops.py#L98

Added line #L98 was not covered by tests
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 @@

@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 @@

@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 @@

@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 @@

@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
Loading