From: whitequark Date: Sun, 12 Apr 2020 03:59:56 +0000 (+0000) Subject: hdl.ast: improve repr() for Shape. X-Git-Tag: working_23jun2020~13 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=e9c75f7ca172d931d11840f129301b90148f3560;p=nmigen.git 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. --- diff --git a/nmigen/hdl/ast.py b/nmigen/hdl/ast.py index cadf960..fa2c9d1 100644 --- a/nmigen/hdl/ast.py +++ b/nmigen/hdl/ast.py @@ -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): diff --git a/nmigen/test/test_hdl_ast.py b/nmigen/test/test_hdl_ast.py index c0d26ee..26e506a 100644 --- a/nmigen/test/test_hdl_ast.py +++ b/nmigen/test/test_hdl_ast.py @@ -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)