From c0bff9d07075d57195df70b82d8463d2a6668efe Mon Sep 17 00:00:00 2001 From: Jim Pivarski Date: Wed, 12 Jan 2022 12:32:42 -0600 Subject: [PATCH] Make highlevel __repr__ safe for typetracers. --- src/awkward/_v2/highlevel.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/awkward/_v2/highlevel.py b/src/awkward/_v2/highlevel.py index 5e53716830..41525ec6de 100644 --- a/src/awkward/_v2/highlevel.py +++ b/src/awkward/_v2/highlevel.py @@ -1247,14 +1247,20 @@ def __repr__(self): import awkward._v2._prettyprint pytype = type(self).__name__ - valuestr = awkward._v2._prettyprint.valuestr(self, 1, 50) + if self._layout.nplike.known_shape and self._layout.nplike.known_data: + valuestr = " " + awkward._v2._prettyprint.valuestr(self, 1, 50) + typestr = repr(str(self.type))[1:-1] + else: + valuestr = "-typetracer" + typestr = repr( + "?? * " + str(self._layout.form.type_from_behavior(self._behavior)) + )[1:-1] length = max(10, 80 - len(pytype) - 10 - len(valuestr)) - typestr = repr(str(self.type))[1:-1] if len(typestr) > length: typestr = "'" + typestr[: length - 3] + "...'" else: typestr = "'" + typestr + "'" - return "<{0} {1} type={2}>".format(pytype, valuestr, typestr) + return "<{0}{1} type={2}>".format(pytype, valuestr, typestr) def show(self, limit_rows=20, limit_cols=80, type=False, stream=sys.stdout): """ @@ -1933,14 +1939,23 @@ def __repr__(self): import awkward._v2._prettyprint pytype = type(self).__name__ - valuestr = awkward._v2._prettyprint.valuestr(self, 1, 50) + if ( + self._layout.array.nplike.known_shape + and self._layout.array.nplike.known_data + ): + valuestr = " " + awkward._v2._prettyprint.valuestr(self, 1, 50) + typestr = repr(str(self.type))[1:-1] + else: + valuestr = "-typetracer" + typestr = repr( + str(self._layout.array.form.type_from_behavior(self._behavior)) + )[1:-1] length = max(10, 80 - len(pytype) - 10 - len(valuestr)) - typestr = repr(str(self.type))[1:-1] if len(typestr) > length: typestr = "'" + typestr[: length - 3] + "...'" else: typestr = "'" + typestr + "'" - return "<{0} {1} type={2}>".format(pytype, valuestr, typestr) + return "<{0}{1} type={2}>".format(pytype, valuestr, typestr) def show(self, limit_rows=20, limit_cols=80, type=False, stream=sys.stdout): """