hdl.ast: improve repr() for Shape.

The default __repr__() from typing.NamedTuple does not include
the module name, so the replacement (which uses the preferred syntax
for specifying these shapes) doesn't either.
This commit is contained in:
whitequark 2020-04-12 03:59:56 +00:00
parent 9055090f65
commit e9c75f7ca1
2 changed files with 10 additions and 0 deletions

View file

@ -89,6 +89,12 @@ class Shape(typing.NamedTuple):
return Shape(width, signed)
raise TypeError("Object {!r} cannot be used as value shape".format(obj))
def __repr__(self):
if self.signed:
return "signed({})".format(self.width)
else:
return "unsigned({})".format(self.width)
# TODO: use dataclasses instead of this hack
def _Shape___init__(self, width=1, signed=False):

View file

@ -39,6 +39,10 @@ class ShapeTestCase(FHDLTestCase):
msg="Width must be a non-negative integer, not -1"):
Shape(-1)
def test_repr(self):
self.assertEqual(repr(Shape()), "unsigned(1)")
self.assertEqual(repr(Shape(2, True)), "signed(2)")
def test_tuple(self):
width, signed = Shape()
self.assertEqual(width, 1)