hdl.ast: improve repr() for Shape.
authorwhitequark <whitequark@whitequark.org>
Sun, 12 Apr 2020 03:59:56 +0000 (03:59 +0000)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 31 Dec 2021 13:25:01 +0000 (13:25 +0000)
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.

nmigen/hdl/ast.py
nmigen/test/test_hdl_ast.py

index cadf9600c6d5cc62c295a53f911a9a19f87a4d58..fa2c9d1597198d86f75d7e9e62b8d5b4b41d57d2 100644 (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):
index c0d26eec9cf99764704177ee3c240c81de741a89..26e506aa551fe435c05a3f28856475de73d5c874 100644 (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)