From cab37604c7a822e43c06ee8acca7ba69397e8d9c Mon Sep 17 00:00:00 2001 From: Christoph Boeddeker Date: Wed, 29 May 2024 16:55:20 +0200 Subject: [PATCH] fix pprint to preserve the original --- paderbox/utils/pretty.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/paderbox/utils/pretty.py b/paderbox/utils/pretty.py index b0a2cc58..7b1becca 100644 --- a/paderbox/utils/pretty.py +++ b/paderbox/utils/pretty.py @@ -113,6 +113,16 @@ def _dataclass_repr_pretty_(self, p, cycle): ) >>> pprint(PointClsWithALongName(1, 2), max_width=len('PointClsWithALongName') + 10) PointClsWithALongName(x=1, y=2) + + >>> @dataclasses.dataclass + ... class PrettyPoint: + ... x: int + ... y: int + ... def _repr_pretty_(self, p, cycle): + ... p.text(f'CustomRepr(x={self.x}, y={self.y})') + >>> pprint(PrettyPoint(1, 2)) + CustomRepr(x=1, y=2) + """ if cycle: p.text(f'{self.__class__.__name__}(...)') @@ -133,7 +143,7 @@ def _dataclass_repr_pretty_(self, p, cycle): p.text(')') def _in_deferred_types(self, cls): - if dataclasses.is_dataclass(cls): + if '_repr_pretty_' not in cls.__dict__ and dataclasses.is_dataclass(cls): return self._dataclass_repr_pretty_ else: return super()._in_deferred_types(cls)