rich icon indicating copy to clipboard operation
rich copied to clipboard

Truncate long object representations in `to_repr`

Open liblaf opened this issue 6 months ago • 0 comments

Type of changes

  • [x] Bug fix
  • [ ] New feature
  • [ ] Documentation / docstrings
  • [ ] Tests
  • [ ] Other

Checklist

  • [x] I've run the latest black with default args on new code.
  • [ ] I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • [ ] I've added tests for new code.
  • [x] I accept that @willmcgugan may be pedantic in the code review.

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 verbose repr() outputs, making tracebacks difficult to read and navigate.

Currently, to_repr truncates strings and bytes if they exceed max_string. This change extends similar truncation logic to the generic repr(obj) output. If max_string is set and the repr() 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!

liblaf avatar Jun 19 '25 10:06 liblaf