Skip to content

Commit 44d5fac

Browse files
committed
lib.wiring: fix equality of FlippedSignature with other object.
Fixes #882.
1 parent cfd4f9c commit 44d5fac

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

amaranth/lib/wiring.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,11 @@ def __eq__(self, other):
484484
return self.flip() == other.flip()
485485
else:
486486
# Delegate comparisons back to Signature (or its descendant) by flipping the arguments;
487-
# equality must be reflexive but the implementation of __eq__ need not be, and we can
488-
# take advantage of it here.
489-
return other == self
487+
# equality must be reflexive but the implementation of `__eq__` need not be, and we can
488+
# take advantage of it here. This is done by returning `NotImplemented`, otherwise if
489+
# the other object cannot be compared to a `FlippedSignature` either this will result
490+
# in infinite recursion.
491+
return NotImplemented
490492

491493
# These methods do not access instance variables and so their implementation can be shared
492494
# between the normal and the flipped member collections.

tests/test_lib_wiring.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,3 +832,15 @@ class C3(Component):
832832
r"a signature member; did you mean 'val2: In\(MockValueCastable\)' or "
833833
r"'val2: Out\(MockValueCastable\)'\?$"):
834834
C3().signature
835+
836+
def test_bug_882(self):
837+
class PageBuffer(Component):
838+
rand: Signature({}).flip()
839+
other: Out(1)
840+
841+
with self.assertWarnsRegex(SyntaxWarning,
842+
r"^Component '.+\.PageBuffer' has an annotation 'rand: Signature\({}\)\.flip\(\)', "
843+
r"which is not a signature member; did you mean "
844+
r"'rand: In\(Signature\({}\)\.flip\(\)\)' or "
845+
r"'rand: Out\(Signature\({}\)\.flip\(\)\)'\?$"):
846+
PageBuffer()

0 commit comments

Comments
 (0)