From b4341be0991e1b3e9e7f945c10d446068e293d2e Mon Sep 17 00:00:00 2001 From: Max Marrone Date: Tue, 19 Mar 2024 16:48:13 -0400 Subject: [PATCH] Add __repr__ to OrderedSet, for debugging. --- api/src/opentrons/ordered_set.py | 6 ++++++ api/tests/opentrons/test_ordered_set.py | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/api/src/opentrons/ordered_set.py b/api/src/opentrons/ordered_set.py index 99b8f77b3d7f..9080b67cf273 100644 --- a/api/src/opentrons/ordered_set.py +++ b/api/src/opentrons/ordered_set.py @@ -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})" diff --git a/api/tests/opentrons/test_ordered_set.py b/api/tests/opentrons/test_ordered_set.py index 0bb620f92be7..768826db3d34 100644 --- a/api/tests/opentrons/test_ordered_set.py +++ b/api/tests/opentrons/test_ordered_set.py @@ -145,3 +145,12 @@ 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: + subject = OrderedSet([1, 2, 3]) + result = repr(subject) + assert "OrderedSet" in result + assert "1" in result + assert "2" in result + assert "3" in result