Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] List of numpy arrays does not print correctly #1876

Closed
wilrop opened this issue Jan 27, 2022 · 10 comments · Fixed by #1950
Closed

[BUG] List of numpy arrays does not print correctly #1876

wilrop opened this issue Jan 27, 2022 · 10 comments · Fixed by #1950
Assignees

Comments

@wilrop
Copy link

wilrop commented Jan 27, 2022

The bug is actually rather simple. Whenever you attempt to print a list that consists of NumPy arrays, it cuts off the first element of the list when combined with a string. Below is a minimal code example that shows the bug:

import numpy as np
from rich import print

list_of_arrays = [np.array([1, 0, 0]), np.array([0, 1, 0]), np.array([0, 0, 1])]
print(list_of_arrays)
print(f'Does something weird with f strings: {list_of_arrays}')

output

[array([1, 0, 0]), array([0, 1, 0]), array([0, 0, 1])]
Does something weird with f strings: ), array([0, 1, 0]), array([0, 0, 1])]

Of course, the output should be that the second line also shows the complete list.
I provide a screenshot below to show what it looks like in my terminal.
image

Platform
Platform: Mac
Terminal: Oh-my-zsh
Python version: 3.7
Rich version: 11.0.0

Click to expand The other information that was asked after executing the commands: ╭───────────────────────── ─────────────────────────╮ │ A high level console interface. │ │ │ │ ╭──────────────────────────────────────────────────────────────────────────────╮ │ │ │ │ │ │ ╰──────────────────────────────────────────────────────────────────────────────╯ │ │ │ │ color_system = '256' │ │ encoding = 'utf-8' │ │ file = <_io.TextIOWrapper name='' mode='w' encoding='UTF-8'> │ │ height = 12 │ │ is_alt_screen = False │ │ is_dumb_terminal = False │ │ is_interactive = True │ │ is_jupyter = False │ │ is_terminal = True │ │ legacy_windows = False │ │ no_color = False │ │ options = ConsoleOptions( │ │ size=ConsoleDimensions(width=197, height=12), │ │ legacy_windows=False, │ │ min_width=1, │ │ max_width=197, │ │ is_terminal=True, │ │ encoding='utf-8', │ │ max_height=12, │ │ justify=None, │ │ overflow=None, │ │ no_wrap=False, │ │ highlight=None, │ │ markup=None, │ │ height=None │ │ ) │ │ quiet = False │ │ record = False │ │ safe_box = True │ │ size = ConsoleDimensions(width=197, height=12) │ │ soft_wrap = False │ │ stderr = False │ │ style = None │ │ tab_size = 8 │ │ width = 197 │ ╰──────────────────────────────────────────────────────────────────────────────────╯

platform="Darwin"
WindowsConsoleFeatures(vt=False, truecolor=False)

I would be happy to provide any additional feedback if necessary.

@sanders41
Copy link
Contributor

It seems to be seeing it as markup.

import numpy as np
from rich import print
from rich.console import Console

console = Console()

list_of_arrays = [np.array([1, 0, 0]), np.array([0, 1, 0]), np.array([0, 0, 1])]
print(list_of_arrays)
print(f'Does something weird with f strings: {list_of_arrays}')
console.print(f'Does something weird with f strings: {list_of_arrays}', markup=False)

output

[array([1, 0, 0]), array([0, 1, 0]), array([0, 0, 1])]
Does something weird with f strings: ), array([0, 1, 0]), array([0, 0, 1])]
Does something weird with f strings: [array([1, 0, 0]), array([0, 1, 0]), array([0, 0, 1])]

@sanders41
Copy link
Contributor

I tracked it down to here.

Nested lists are being matched in the regular expression so it isn't specific to NumPy. Any function, class, etc with a list matches. For example:

from array import array
from rich import print

test_list = [array("l", [1, 2]), array("l", [3, 4])]
print(f'test {test_list}')

output

test ), array('l', [3, 4])]

I can't currently think of a way to fix this without breaking the markkup. With the current regular expression an escape can be used to prevent the issue.

import numpy as np
from rich import print

list_of_arrays = [np.array([1, 0, 0]), np.array([0, 1, 0]), np.array([0, 0, 1])]
print(f'Does something weird with f strings: \{list_of_arrays}')

output

Does something weird with f strings: [array([1, 0, 0]), array([0, 1, 0]), array([0, 0, 1])]

@willmcgugan
Copy link
Collaborator

@sanders41 is absolutely correct. It's because the output happens to look like a console markup tag.

One option is to escape the value in the f string. Import escape from rich.markup, then do {escape(str(list_of_arrays))}.

Alternatively, if you use a Console object, you can disable markup, either globally or on the individual print.

However, you're better off not converting the object to a string at all. If you just print the array, Rich can format and highlight it without escaping issues:

print("Don't do anything with f string:", list_of_arrays)

@willmcgugan
Copy link
Collaborator

The solution above is probably best. But because this is a common, and natural, thing to do in Python we are going to look at excluding tags that contain parenthesis. This should prevent accidental markup processsing.

@wilrop
Copy link
Author

wilrop commented Jan 31, 2022

Thanks for the follow-up. Excited to see what comes out of this!

@github-actions
Copy link

Did I solve your problem?

Why not buy the devs a coffee to say thanks?

@wilrop
Copy link
Author

wilrop commented Mar 10, 2022

I have just retested this old bit of code and it seems to be broken still/again?

Proof:
image

I'm on Rich 11.2.0 with an M1 mac if that makes a difference.

@willmcgugan
Copy link
Collaborator

We haven't released a new version yet...

@wilrop
Copy link
Author

wilrop commented Mar 10, 2022

I'm absolutely stupid, for some reason I thought it would already be in there. I'm very sorry!

@willmcgugan
Copy link
Collaborator

New version landing in a few days :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants