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:
parent
35561ea11a
commit
14e73a73de
|
@ -133,13 +133,8 @@ class Shape:
|
||||||
return "unsigned({})".format(self.width)
|
return "unsigned({})".format(self.width)
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if not isinstance(other, Shape):
|
return (isinstance(other, Shape) and
|
||||||
try:
|
self.width == other.width and self.signed == other.signed)
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def unsigned(width):
|
def unsigned(width):
|
||||||
|
|
|
@ -38,7 +38,8 @@ class Field:
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return (isinstance(other, Field) and
|
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):
|
def __repr__(self):
|
||||||
return f"Field({self._shape!r}, {self._offset})"
|
return f"Field({self._shape!r}, {self._offset})"
|
||||||
|
|
|
@ -47,15 +47,8 @@ class ShapeTestCase(FHDLTestCase):
|
||||||
r"^Width must be a non-negative integer, not -1$"):
|
r"^Width must be a non-negative integer, not -1$"):
|
||||||
Shape(-1)
|
Shape(-1)
|
||||||
|
|
||||||
def test_compare_wrong(self):
|
def test_compare_non_shape(self):
|
||||||
with self.assertRaisesRegex(TypeError,
|
self.assertNotEqual(Shape(1, True), "hi")
|
||||||
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_repr(self):
|
def test_repr(self):
|
||||||
self.assertEqual(repr(Shape()), "unsigned(1)")
|
self.assertEqual(repr(Shape()), "unsigned(1)")
|
||||||
|
|
|
@ -534,7 +534,7 @@ class StructTestCase(FHDLTestCase):
|
||||||
|
|
||||||
v = S()
|
v = S()
|
||||||
self.assertEqual(Layout.of(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.assertEqual(Value.cast(v).name, "v")
|
||||||
self.assertRepr(v.a, "(slice (sig v) 0:1)")
|
self.assertRepr(v.a, "(slice (sig v) 0:1)")
|
||||||
self.assertRepr(v.b, "(s (slice (sig v) 1:4))")
|
self.assertRepr(v.b, "(s (slice (sig v) 1:4))")
|
||||||
|
@ -550,7 +550,7 @@ class StructTestCase(FHDLTestCase):
|
||||||
a: unsigned(1)
|
a: unsigned(1)
|
||||||
b: R
|
b: R
|
||||||
|
|
||||||
self.assertEqual(S, unsigned(9))
|
self.assertEqual(Shape.cast(S), unsigned(9))
|
||||||
|
|
||||||
v = S()
|
v = S()
|
||||||
self.assertIs(Layout.of(v), S)
|
self.assertIs(Layout.of(v), S)
|
||||||
|
@ -654,7 +654,7 @@ class UnionTestCase(FHDLTestCase):
|
||||||
|
|
||||||
v = U()
|
v = U()
|
||||||
self.assertEqual(Layout.of(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.a, "(slice (sig v) 0:1)")
|
||||||
self.assertRepr(v.b, "(s (slice (sig v) 0:3))")
|
self.assertRepr(v.b, "(s (slice (sig v) 0:3))")
|
||||||
|
|
||||||
|
@ -803,7 +803,7 @@ class RFCExamplesTestCase(TestCase):
|
||||||
kind: Kind
|
kind: Kind
|
||||||
value: Value
|
value: Value
|
||||||
|
|
||||||
self.assertEqual(SomeVariant, unsigned(3))
|
self.assertEqual(Shape.cast(SomeVariant), unsigned(3))
|
||||||
|
|
||||||
view3 = SomeVariant()
|
view3 = SomeVariant()
|
||||||
self.assertIsInstance(Value.cast(view3), Signal)
|
self.assertIsInstance(Value.cast(view3), Signal)
|
||||||
|
|
Loading…
Reference in a new issue