question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] When pretty-printed, items with a multi-line repr are not indented

See original GitHub issue

Describe 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:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
willmcgugancommented, Apr 22, 2022

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.

1reaction
rodrigogiraoserraocommented, Apr 22, 2022

It would be nice if Rich could do better.

Agreed.

I think that if there is even a single repr with new lines we should expand everything, i.e. don’t try to fit it on a single line. When there is a multi-line repr it should be indented so that it lines up the original output.

What should Rich do? Two alternatives below:

{
│   'foo': 'bar',
│   'zeros': array([[0., 0., 0., 0.],
│                   [0., 0., 0., 0.],
│                   [0., 0., 0., 0.],
│                   [0., 0., 0., 0.]])
}
{
│   'foo': 'bar',
│   'zeros': 
|   |   array([[0., 0., 0., 0.],
│   |          [0., 0., 0., 0.],
│   |          [0., 0., 0., 0.],
│   |          [0., 0., 0., 0.]])
}

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…?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proper indentation for multiline strings? - python
Docstrings are treated specially: any indent of the first line is removed; the smallest common indent taken over all other non-blank lines is...
Read more >
pprint — Data pretty printer — Python 3.11.1 documentation
The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don't fit within...
Read more >
8.11. pprint — Data pretty printer
If not specified, the PrettyPrinter adopts sys.stdout . The amount of indentation added for each recursive level is specified by indent; the default...
Read more >
CoffeeScript
The core compiler however, does not depend on Node, and can be run in any ... other block expressions, multi-line conditionals are delimited...
Read more >
How to Pretty-Print Pandas DataFrames and Series
Whether to print out the full DataFrame repr for wide DataFrames across multiple lines, max_columns is still respected, but the output will ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found