Truncate long object representations in to_repr
#3773
+2
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Type of changes
Checklist
Description
Hi Rich maintainers,
First off, thank you for creating and maintaining
rich
– it's an incredibly useful library that I use frequently!This PR addresses the issue of very long object representations in tracebacks, especially when
show_locals=True
is enabled. Objects like large NumPy arrays, PyTorch tensors, or JAX arrays can produce extremely verboserepr()
outputs, making tracebacks difficult to read and navigate.Currently,
to_repr
truncates strings and bytes if they exceedmax_string
. This change extends similar truncation logic to the genericrepr(obj)
output. Ifmax_string
is set and therepr()
of an object exceeds this length, it will be truncated with an ellipsis (...
).This is a partial solution that helps improve readability for general objects whose
repr()
is too long. It doesn't allow for type-specific custom representations, which has been discussed in issues like #3393 and PR #3394. I believe allowing external registration of__rich_repr__
or similar custom display functions would be a more comprehensive solution, as mentioned in discussion #1258. See discussion #3774.However, I hope this small change can provide an immediate improvement for users dealing with overly long representations in tracebacks.
Thank you for your consideration and for all your work on Rich!