You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First, a huge thank you to the Textualize team for rich! It makes CLI outputs and debugging so much more pleasant.
I've been exploring ways to improve the display of objects in tracebacks when show_locals=True is active, particularly for objects from libraries like NumPy, PyTorch, or JAX, which can have very long string representations. This can make tracebacks quite overwhelming. Although there is numpy.set_printoptions, the representations of large nested arrays are still too long.
While the existing max_string option in rich.pretty.Pretty (and by extension, tracebacks) helps, and the recent PR#3773 I submitted (if merged) would extend truncation to generic repr() outputs, these don't offer fine-grained control over how different types of objects are represented. Ideally, we could have more concise, type-specific summaries, like showing f64[10,10](numpy) for a NumPy array instead of its full content.
This would then display arr in the locals section in a more compact form, like f64[10,10](numpy) (depending on the wadler_lindig.pformat output or your custom formatter).
This approach could be extended using functools.singledispatch or a library like plum for a more organized dispatching registry for different types.
Again, this is a workaround with significant caveats. I'm sharing it to illustrate the kind of control that would be beneficial and to perhaps spark further discussion on how a more elegant, integrated solution could be achieved within rich itself. A built-in mechanism for registering external pretty-printers/repr functions would be a very welcome addition.
What are others' thoughts on this? Are there other workarounds people are using? And what's the appetite for a more official solution along these lines?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
First, a huge thank you to the Textualize team for
rich
! It makes CLI outputs and debugging so much more pleasant.I've been exploring ways to improve the display of objects in tracebacks when
show_locals=True
is active, particularly for objects from libraries like NumPy, PyTorch, or JAX, which can have very long string representations. This can make tracebacks quite overwhelming. Although there isnumpy.set_printoptions
, the representations of large nested arrays are still too long.While the existing
max_string
option inrich.pretty.Pretty
(and by extension, tracebacks) helps, and the recent PR#3773 I submitted (if merged) would extend truncation to genericrepr()
outputs, these don't offer fine-grained control over how different types of objects are represented. Ideally, we could have more concise, type-specific summaries, like showingf64[10,10](numpy)
for a NumPy array instead of its full content.The discussion around Register custom
__repr__
functions outside of their classes · Issue #3393 and Make it possible to override the representation function from the outside by danijar · Pull Request #3394 highlights the desire for a more robust solution, perhaps allowing users to register custom__rich_repr__
or equivalent display functions externally. This would be fantastic as we can't always modify third-party library classes.In the meantime, I've been experimenting with a workaround that involves monkey-patching
builtins.repr
.Warning
I want to stress that monkey-patching
builtins
is generally a bad idea and can have unintended side effects.However, for those who are struggling with this issue and understand the risks, it might offer a temporary solution.
Here's an example of how one might achieve custom representations for NumPy arrays using this method with
rich.traceback
:This would then display
arr
in the locals section in a more compact form, likef64[10,10](numpy)
(depending on thewadler_lindig.pformat
output or your custom formatter).Output Example:
This approach could be extended using
functools.singledispatch
or a library likeplum
for a more organized dispatching registry for different types.Again, this is a workaround with significant caveats. I'm sharing it to illustrate the kind of control that would be beneficial and to perhaps spark further discussion on how a more elegant, integrated solution could be achieved within
rich
itself. A built-in mechanism for registering external pretty-printers/repr functions would be a very welcome addition.What are others' thoughts on this? Are there other workarounds people are using? And what's the appetite for a more official solution along these lines?
Thanks again for the wonderful library!
Beta Was this translation helpful? Give feedback.
All reactions