Skip to content

Commit 14e73a7

Browse files
committed
hdl.ast: do not cast comparand to shape in Shape.__eq__.
This doesn't match how other Python comparison operators work. E.g. `1 == int("1")` but `1 != "1"`.
1 parent 35561ea commit 14e73a7

File tree

4 files changed

+10
-21
lines changed

4 files changed

+10
-21
lines changed

amaranth/hdl/ast.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,8 @@ def __repr__(self):
133133
return "unsigned({})".format(self.width)
134134

135135
def __eq__(self, other):
136-
if not isinstance(other, Shape):
137-
try:
138-
other = self.__class__.cast(other)
139-
except TypeError as e:
140-
raise TypeError("Shapes may be compared with shape-castable objects, not {!r}"
141-
.format(other)) from e
142-
return self.width == other.width and self.signed == other.signed
136+
return (isinstance(other, Shape) and
137+
self.width == other.width and self.signed == other.signed)
143138

144139

145140
def unsigned(width):

amaranth/lib/data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def width(self):
3838

3939
def __eq__(self, other):
4040
return (isinstance(other, Field) and
41-
self._shape == other.shape and self._offset == other.offset)
41+
Shape.cast(self._shape) == Shape.cast(other.shape) and
42+
self._offset == other.offset)
4243

4344
def __repr__(self):
4445
return f"Field({self._shape!r}, {self._offset})"

tests/test_hdl_ast.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,8 @@ def test_make_wrong(self):
4747
r"^Width must be a non-negative integer, not -1$"):
4848
Shape(-1)
4949

50-
def test_compare_wrong(self):
51-
with self.assertRaisesRegex(TypeError,
52-
r"^Shapes may be compared with shape-castable objects, not 'hi'$"):
53-
Shape(1, True) == 'hi'
54-
55-
def test_compare_tuple_wrong(self):
56-
with self.assertRaisesRegex(TypeError,
57-
r"^Shapes may be compared with shape-castable objects, not \(2, 3\)$"):
58-
Shape(1, True) == (2, 3)
50+
def test_compare_non_shape(self):
51+
self.assertNotEqual(Shape(1, True), "hi")
5952

6053
def test_repr(self):
6154
self.assertEqual(repr(Shape()), "unsigned(1)")

tests/test_lib_data.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ class S(Struct):
534534

535535
v = S()
536536
self.assertEqual(Layout.of(v), S)
537-
self.assertEqual(Value.cast(v).shape(), S)
537+
self.assertEqual(Value.cast(v).shape(), Shape.cast(S))
538538
self.assertEqual(Value.cast(v).name, "v")
539539
self.assertRepr(v.a, "(slice (sig v) 0:1)")
540540
self.assertRepr(v.b, "(s (slice (sig v) 1:4))")
@@ -550,7 +550,7 @@ class S(Struct):
550550
a: unsigned(1)
551551
b: R
552552

553-
self.assertEqual(S, unsigned(9))
553+
self.assertEqual(Shape.cast(S), unsigned(9))
554554

555555
v = S()
556556
self.assertIs(Layout.of(v), S)
@@ -654,7 +654,7 @@ class U(Union):
654654

655655
v = U()
656656
self.assertEqual(Layout.of(v), U)
657-
self.assertEqual(Value.cast(v).shape(), U)
657+
self.assertEqual(Value.cast(v).shape(), Shape.cast(U))
658658
self.assertRepr(v.a, "(slice (sig v) 0:1)")
659659
self.assertRepr(v.b, "(s (slice (sig v) 0:3))")
660660

@@ -803,7 +803,7 @@ class Value(Union):
803803
kind: Kind
804804
value: Value
805805

806-
self.assertEqual(SomeVariant, unsigned(3))
806+
self.assertEqual(Shape.cast(SomeVariant), unsigned(3))
807807

808808
view3 = SomeVariant()
809809
self.assertIsInstance(Value.cast(view3), Signal)

0 commit comments

Comments
 (0)