Skip to content

Commit 5d9222a

Browse files
committed
Implement a __eq__ method for _power_managing.Proposal
Earlier a default `eq` implementation was conflicting with the custom `lt` implementation on what properties were used to compare two proposals. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 264d16f commit 5d9222a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/frequenz/sdk/actor/_power_managing/_base_classes.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,27 @@ def __lt__(self, other: Proposal) -> bool:
231231
self.priority == other.priority and self.source_id < other.source_id
232232
)
233233

234+
def __eq__(self, other: object) -> bool:
235+
"""Check if two proposals are equal.
236+
237+
Equality is determined by the priority and source ID of the proposals, so
238+
two proposals are equal if they have the same priority and source ID, even
239+
if they have different power values or creation times.
240+
241+
This is so that there is only one active proposal for each actor in the bucket,
242+
and older proposals are replaced by newer ones.
243+
244+
Args:
245+
other: The other proposal to compare to.
246+
247+
Returns:
248+
Whether the two proposals are equal.
249+
"""
250+
if not isinstance(other, Proposal):
251+
return NotImplemented
252+
253+
return self.priority == other.priority and self.source_id == other.source_id
254+
234255

235256
class Algorithm(enum.Enum):
236257
"""The available algorithms for the power manager."""

0 commit comments

Comments
 (0)