From d89a7b43bcdfef2b7c35bc3c0a9a43592563d279 Mon Sep 17 00:00:00 2001 From: Zhenjia Xu Date: Thu, 26 Dec 2024 23:07:36 -0800 Subject: [PATCH] fix repr in ipython --- genesis/datatypes.py | 4 ---- genesis/repr_base.py | 13 +++++++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/genesis/datatypes.py b/genesis/datatypes.py index 83960362..c15b0442 100644 --- a/genesis/datatypes.py +++ b/genesis/datatypes.py @@ -72,10 +72,6 @@ def _repr_brief(self): return repr_str - def __repr__(self): - if not __debug__: - self.__colorized__repr__() - def __colorized__repr__(self): repr_str = f"{colors.BLUE}{self._repr_type()}(len={colors.MINT}{formats.UNDERLINE}{len(self)}{formats.RESET}{colors.BLUE}, [" diff --git a/genesis/repr_base.py b/genesis/repr_base.py index 86f18cf0..b81d0e07 100644 --- a/genesis/repr_base.py +++ b/genesis/repr_base.py @@ -1,3 +1,5 @@ +import inspect + import genesis as gs import genesis.utils.repr as ru from genesis.styles import colors, formats, styless @@ -31,9 +33,16 @@ def _repr_brief(self): repr_str += f", material: {self.material}" return repr_str + def _is_debugger(self) -> bool: + """Detect if running under a debugger (VSCode or PyCharm).""" + for frame in inspect.stack(): + if any(module in frame.filename for module in ("debugpy", "ptvsd", "pydevd")): + return True + return False + def __repr__(self): - if not __debug__: - self.__colorized__repr__() + if not self._is_debugger(): + return self.__colorized__repr__() def __colorized__repr__(self) -> str: all_attrs = self.__dir__()