Skip to content

Commit

Permalink
fix: Equality check between Sum types (#1422)
Browse files Browse the repository at this point in the history
`Sum`, `Tuple` and `UnitSum` can represent the same type, but the
equality fn checked the specific subclasses.
  • Loading branch information
aborgna-q authored Aug 13, 2024
1 parent 3bf968f commit 8dfea09
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 6 additions & 3 deletions hugr-py/src/hugr/tys.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,18 @@ def as_tuple(self) -> Tuple:
def __repr__(self) -> str:
return f"Sum({self.variant_rows})"

def __eq__(self, other: object) -> bool:
return isinstance(other, Sum) and self.variant_rows == other.variant_rows

def type_bound(self) -> TypeBound:
return TypeBound.join(*(t.type_bound() for r in self.variant_rows for t in r))


@dataclass()
@dataclass(eq=False)
class UnitSum(Sum):
"""Simple :class:`Sum` type with `size` variants of empty rows."""

size: int
size: int = field(compare=False)

def __init__(self, size: int):
self.size = size
Expand All @@ -262,7 +265,7 @@ def __repr__(self) -> str:
return f"UnitSum({self.size})"


@dataclass()
@dataclass(eq=False)
class Tuple(Sum):
"""Product type with `tys` elements. Instances of this type correspond to
:class:`Sum` with a single variant.
Expand Down
14 changes: 14 additions & 0 deletions hugr-py/tests/test_tys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations

from hugr.tys import Bool, Qubit, Sum, Tuple, UnitSum


def test_sums():
assert Sum([[Bool, Qubit]]) == Tuple(Bool, Qubit)
assert Tuple(Bool, Qubit) == Sum([[Bool, Qubit]])
assert Sum([[Bool, Qubit]]).as_tuple() == Sum([[Bool, Qubit]])

assert Tuple() == Sum([[]])
assert UnitSum(0) == Sum([])
assert UnitSum(1) == Tuple()
assert UnitSum(4) == Sum([[], [], [], []])

0 comments on commit 8dfea09

Please sign in to comment.