[BUG] When pretty-printed, items with a multi-line repr are not indented
See original GitHub issueDescribe the bug
Not entirely sure if it’s a proper bug or a won’t fix, but at least getting an answer would be great.
Context: I’m using Rich to display nicely a hierarchy of attrs
-powered objects using rich.pretty.pprint()
. This works very well and Rich saves me a lot of trouble when visually inspecting those objects.
Some of the fields in my objects consist of Numpy or xarray data whose repr may span accros multiple lines. Here is an example:
import numpy as np
from rich.pretty import pprint
zeros = np.zeros((4,4))
pprint(zeros)
I get a nicely indented repr:
array([[0., 0., 0., 0.],
│ [0., 0., 0., 0.],
│ [0., 0., 0., 0.],
│ [0., 0., 0., 0.]])
Now, if I use one of these in a dictionary, like this
d = {"foo": "bar", "zeros": zeros}
pprint(d)
I get that
{
│ 'foo': 'bar',
│ 'zeros': array([[0., 0., 0., 0.],
│ [0., 0., 0., 0.],
│ [0., 0., 0., 0.],
│ [0., 0., 0., 0.]])
}
while I’d expect indentation to be adjusted like this
{
│ 'foo': 'bar',
│ 'zeros': array([[0., 0., 0., 0.],
│ [0., 0., 0., 0.],
│ [0., 0., 0., 0.],
│ [0., 0., 0., 0.]])
}
or at least something like this (saves horizontal space, no need to guess length of first line)
{
│ 'foo': 'bar',
│ 'zeros':
| | array([[0., 0., 0., 0.],
│ | [0., 0., 0., 0.],
│ | [0., 0., 0., 0.],
│ | [0., 0., 0., 0.]])
}
I’m showing here an example with a dict, but this also applies when pretty printing attrs
objects with fields containing Numpy or xarray data.
Platform
Click to expand
Platform: macOS, Linux Rich version: 12.0.0 Output: Terminal, PyCharm and VSCode embedded consoles, Jupyter Lab (intentionally not cluttering the issue with terminal info, I can add them if it’s relevant)
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
I think the first one. It should parse as Python code.
I agree that that doesn’t need the extra guidelines, because the guides indicate the level of the dict, not the whitespace to align the matrix.
Agreed.
What should Rich do? Two alternatives below:
And should the array itself (or the other items with multi-line reprs) have guidelines? I don’t think so, because the multi-line repr itself might not be indented or aligned and the guidelines might end up looking awkward…?