From bbb518a2b2632b5181b40f0125a6c39c8416da79 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 | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/api/src/opentrons/ordered_set.py b/api/src/opentrons/ordered_set.py index 99b8f77b3d7..9080b67cf27 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 0bb620f92be..48f50bc79b7 100644 --- a/api/tests/opentrons/test_ordered_set.py +++ b/api/tests/opentrons/test_ordered_set.py @@ -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