Skip to content

Commit

Permalink
Add __str__ impls and more truthful __repr__
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Aug 29, 2024
1 parent 638d019 commit 79b99f7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
13 changes: 12 additions & 1 deletion hugr-py/src/hugr/tys.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,23 @@ class Either(Sum):
In fallible contexts, the Left variant is used to represent success, and the
Right variant is used to represent failure.
Example:
>>> either = Either([Bool, Bool], [Bool])
>>> either
Either(left=[Bool, Bool], right=[Bool])
>>> str(either)
'Either((Bool, Bool), Bool)'
"""

def __init__(self, left: Iterable[Type], right: Iterable[Type]):
self.variant_rows = [list(left), list(right)]

def __repr__(self) -> str:
def __repr__(self) -> str: # pragma: no cover
left, right = self.variant_rows
return f"Either(left={left}, right={right})"

def __str__(self) -> str:
left, right = self.variant_rows
left_str = left[0] if len(left) == 1 else tuple(left)
right_str = right[0] if len(right) == 1 else tuple(right)
Expand Down
45 changes: 29 additions & 16 deletions hugr-py/src/hugr/val.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ class Some(Sum):
>>> some = Some(TRUE, FALSE)
>>> some
Some(TRUE, FALSE)
>>> str(some)
'Some(TRUE, FALSE)'
>>> some.type_()
Option(Bool, Bool)
Expand All @@ -185,6 +187,8 @@ class None_(Sum):
>>> none = None_(tys.Bool)
>>> none
None(Bool)
>>> str(none)
'None'
>>> none.type_()
Option(Bool)
Expand All @@ -196,6 +200,9 @@ def __init__(self, *types: tys.Type):
def __repr__(self) -> str:
return f"None({', '.join(map(repr, self.typ.variant_rows[0]))})"

def __str__(self) -> str:
return "None"


@dataclass
class Left(Sum):
Expand All @@ -206,27 +213,30 @@ class Left(Sum):
Example:
>>> left = Left([TRUE, FALSE], [tys.Bool])
>>> left
Left((TRUE, FALSE), Bool)
>>> left.type_()
Either((Bool, Bool), Bool)
Left(vals=[TRUE, FALSE], right_typ=[Bool])
>>> str(left)
'Left(TRUE, FALSE)'
>>> str(left.type_())
'Either((Bool, Bool), Bool)'
"""

#: The values of this tuple.
vals: list[Value]

def __init__(self, vals: Iterable[Value], left_typ: Iterable[tys.Type]):
def __init__(self, vals: Iterable[Value], right_typ: Iterable[tys.Type]):
val_list = list(vals)
super().__init__(
tag=0,
typ=tys.Either([v.type_() for v in val_list], left_typ),
typ=tys.Either([v.type_() for v in val_list], right_typ),
vals=val_list,
)

def __repr__(self) -> str:
vals_str = self.vals[0] if len(self.vals) == 1 else tuple(self.vals)
_, right = self.typ.variant_rows
right_str = right[0] if len(right) == 1 else tuple(right)
return f"Left({vals_str}, {right_str})"
_, right_typ = self.typ.variant_rows
return f"Left(vals={self.vals}, right_typ={list(right_typ)})"

def __str__(self) -> str:
return f"Left({", ".join(map(str, self.vals))})"


@dataclass
Expand All @@ -240,9 +250,11 @@ class Right(Sum):
Example:
>>> right = Right([tys.Bool, tys.Bool, tys.Bool], [TRUE, FALSE])
>>> right
Right((Bool, Bool, Bool), (TRUE, FALSE))
>>> right.type_()
Either((Bool, Bool, Bool), (Bool, Bool))
Right(left_typ=[Bool, Bool, Bool], vals=[TRUE, FALSE])
>>> str(right)
'Right(TRUE, FALSE)'
>>> str(right.type_())
'Either((Bool, Bool, Bool), (Bool, Bool))'
"""

#: The values of this tuple.
Expand All @@ -257,10 +269,11 @@ def __init__(self, left_typ: Iterable[tys.Type], vals: Iterable[Value]):
)

def __repr__(self) -> str:
left, _ = self.typ.variant_rows
left_str = left[0] if len(left) == 1 else tuple(left)
vals_str = self.vals[0] if len(self.vals) == 1 else tuple(self.vals)
return f"Right({left_str}, {vals_str})"
left_typ, _ = self.typ.variant_rows
return f"Right(left_typ={list(left_typ)}, vals={self.vals})"

def __str__(self) -> str:
return f"Right({", ".join(map(str, self.vals))})"


@dataclass
Expand Down

0 comments on commit 79b99f7

Please sign in to comment.