Skip to content

Commit

Permalink
Add __repr__ to OrderedSet, for debugging.
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxColoring committed Mar 19, 2024
1 parent e0e1259 commit bbb518a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/src/opentrons/ordered_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,9 @@ def __sub__(
The elements that aren't removed retain their original relative order.
"""
return OrderedSet(e for e in self if e not in other)

def __repr__(self) -> str: # noqa: D105
# Use repr() on the keys view in case it's super long and Python is smart
# enough to abbreviate it.
elements_str = repr(self._elements.keys())
return f"OrderedSet({elements_str})"
10 changes: 10 additions & 0 deletions api/tests/opentrons/test_ordered_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,13 @@ def test_difference() -> None:
b = {1, 9}

assert (a - OrderedSet(b)) == (a - b) == OrderedSet([3, 4, 5, 2, 6, 5, 3, 5, 8, 7])


def test_repr() -> None:
"""It should return a meaningful repr string."""
subject = OrderedSet([1, 2, 3])
result = repr(subject)
assert "OrderedSet" in result
assert "1" in result
assert "2" in result
assert "3" in result

0 comments on commit bbb518a

Please sign in to comment.