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"`.
This commit is contained in:
Catherine 2023-02-28 15:52:50 +00:00
parent 35561ea11a
commit 14e73a73de
4 changed files with 10 additions and 21 deletions

View file

@ -133,13 +133,8 @@ class Shape:
return "unsigned({})".format(self.width)
def __eq__(self, other):
if not isinstance(other, Shape):
try:
other = self.__class__.cast(other)
except TypeError as e:
raise TypeError("Shapes may be compared with shape-castable objects, not {!r}"
.format(other)) from e
return self.width == other.width and self.signed == other.signed
return (isinstance(other, Shape) and
self.width == other.width and self.signed == other.signed)
def unsigned(width):

View file

@ -38,7 +38,8 @@ class Field:
def __eq__(self, other):
return (isinstance(other, Field) and
self._shape == other.shape and self._offset == other.offset)
Shape.cast(self._shape) == Shape.cast(other.shape) and
self._offset == other.offset)
def __repr__(self):
return f"Field({self._shape!r}, {self._offset})"

View file

@ -47,15 +47,8 @@ class ShapeTestCase(FHDLTestCase):
r"^Width must be a non-negative integer, not -1$"):
Shape(-1)
def test_compare_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Shapes may be compared with shape-castable objects, not 'hi'$"):
Shape(1, True) == 'hi'
def test_compare_tuple_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Shapes may be compared with shape-castable objects, not \(2, 3\)$"):
Shape(1, True) == (2, 3)
def test_compare_non_shape(self):
self.assertNotEqual(Shape(1, True), "hi")
def test_repr(self):
self.assertEqual(repr(Shape()), "unsigned(1)")

View file

@ -534,7 +534,7 @@ class StructTestCase(FHDLTestCase):
v = S()
self.assertEqual(Layout.of(v), S)
self.assertEqual(Value.cast(v).shape(), S)
self.assertEqual(Value.cast(v).shape(), Shape.cast(S))
self.assertEqual(Value.cast(v).name, "v")
self.assertRepr(v.a, "(slice (sig v) 0:1)")
self.assertRepr(v.b, "(s (slice (sig v) 1:4))")
@ -550,7 +550,7 @@ class StructTestCase(FHDLTestCase):
a: unsigned(1)
b: R
self.assertEqual(S, unsigned(9))
self.assertEqual(Shape.cast(S), unsigned(9))
v = S()
self.assertIs(Layout.of(v), S)
@ -654,7 +654,7 @@ class UnionTestCase(FHDLTestCase):
v = U()
self.assertEqual(Layout.of(v), U)
self.assertEqual(Value.cast(v).shape(), U)
self.assertEqual(Value.cast(v).shape(), Shape.cast(U))
self.assertRepr(v.a, "(slice (sig v) 0:1)")
self.assertRepr(v.b, "(s (slice (sig v) 0:3))")
@ -803,7 +803,7 @@ class RFCExamplesTestCase(TestCase):
kind: Kind
value: Value
self.assertEqual(SomeVariant, unsigned(3))
self.assertEqual(Shape.cast(SomeVariant), unsigned(3))
view3 = SomeVariant()
self.assertIsInstance(Value.cast(view3), Signal)