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
TLDR: Overriding __repr__ of Python types is not always possible (and if it does, may change library behavior unexpectedly), so it would be valuable if rich provided a way to set custom formatters. For example, using a custom __repr__ for Numpy arrays is currently impossible and would become possible with this feature.
How would you improve Rich?
Add a function to register custom representation functions with rich, e.g.:
We're using rich.traceback(show_locals=True) in a large code base that makes heavy use of Numpy and JAX. While it's generally very helpful, it frequently prints arrays that take up a whole screen height in the stack trace. As a result, we find ourselves repeatedly commenting the rich traceback hook in and out.
For our own Python classes, we could just implement __repr__ or __rich_repr__. However, for Python objects of external dependencies, we'd have to monkey-patch these, which poses the rich of changing behavior in unexpected ways. More importantly, the methods cannot be overridden for np.ndarray or other types implemented in C unless the library provides a mechanism for that (which Numpy doesn't).
Moreover, monkey-patching a solution in rich.pretty from the outside is difficult, because I believe the function that needs to be changed is to_repr() inside traverse() in rich/pretty.py, and changing a nested function from the outside does not work (because it is redefined each time the outer function runs).
The suggested feature would enable users to adjust the tracebacks (and pretty printing in general) to their needs, including formatting of Numpy arrays which is currently not possible, and without risk of affecting code behavior.
The text was updated successfully, but these errors were encountered:
that could be extended with a new show_locals_predicate (or something) argument to traceback.install .
(Alternatively widen the type of show_locals to bool | Callable)
TLDR: Overriding
__repr__
of Python types is not always possible (and if it does, may change library behavior unexpectedly), so it would be valuable if rich provided a way to set custom formatters. For example, using a custom__repr__
for Numpy arrays is currently impossible and would become possible with this feature.How would you improve Rich?
Add a function to register custom representation functions with rich, e.g.:
Example usage:
Or equivalently:
What problem does it solve for you?
We're using
rich.traceback(show_locals=True)
in a large code base that makes heavy use of Numpy and JAX. While it's generally very helpful, it frequently prints arrays that take up a whole screen height in the stack trace. As a result, we find ourselves repeatedly commenting the rich traceback hook in and out.For our own Python classes, we could just implement
__repr__
or__rich_repr__
. However, for Python objects of external dependencies, we'd have to monkey-patch these, which poses the rich of changing behavior in unexpected ways. More importantly, the methods cannot be overridden fornp.ndarray
or other types implemented in C unless the library provides a mechanism for that (which Numpy doesn't).Moreover, monkey-patching a solution in
rich.pretty
from the outside is difficult, because I believe the function that needs to be changed isto_repr()
insidetraverse()
inrich/pretty.py
, and changing a nested function from the outside does not work (because it is redefined each time the outer function runs).The suggested feature would enable users to adjust the tracebacks (and pretty printing in general) to their needs, including formatting of Numpy arrays which is currently not possible, and without risk of affecting code behavior.
The text was updated successfully, but these errors were encountered: