Textualize / rich

Rich is a Python library for rich text and beautiful formatting in the terminal.
https://rich.readthedocs.io/en/latest/
MIT License
48.13k stars 1.69k forks source link

[BUG] wrong string representation of MagicsDisplay is used in ipython extension #3317

Open iloveitaly opened 3 months ago

iloveitaly commented 3 months ago

Describe the bug

When rich is installed as an ipython plugin output %lsmagic fails to output anything meaningful:

In [21]: %lsmagic
Out[21]: <IPython.core.magics.basic.MagicsDisplay object at 0x10d0a39e0>

This is because __repr__ instead of __str__ is being used on the MagicsDisplay object. The MagicsDisplay have a special method _repr_pretty_. If you update MagicsDisplay and include:

    def __repr__(self):
        return self._lsmagic()

All is well.

Should rich look for _repr_pretty_ and use instead of __str__ or should ipython update MagicsDisplay?

Platform

Click to expand ``` ╭───────────────────────── ─────────────────────────╮ │ A high level console interface. │ │ │ │ ╭──────────────────────────────────────────────────────────────────────────────╮ │ │ │ │ │ │ ╰──────────────────────────────────────────────────────────────────────────────╯ │ │ │ │ color_system = 'truecolor' │ │ encoding = 'utf-8' │ │ file = <_io.TextIOWrapper name='' mode='w' encoding='utf-8'> │ │ height = 53 │ │ 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=140, height=53), │ │ legacy_windows=False, │ │ min_width=1, │ │ max_width=140, │ │ is_terminal=True, │ │ encoding='utf-8', │ │ max_height=53, │ │ justify=None, │ │ overflow=None, │ │ no_wrap=False, │ │ highlight=None, │ │ markup=None, │ │ height=None │ │ ) │ │ quiet = False │ │ record = False │ │ safe_box = True │ │ size = ConsoleDimensions(width=140, height=53) │ │ soft_wrap = False │ │ stderr = False │ │ style = None │ │ tab_size = 8 │ │ width = 140 │ ╰──────────────────────────────────────────────────────────────────────────────────╯ ╭─── ────╮ │ Windows features available. │ │ │ │ ╭───────────────────────────────────────────────────╮ │ │ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │ │ ╰───────────────────────────────────────────────────╯ │ │ │ │ truecolor = False │ │ vt = False │ ╰───────────────────────────────────────────────────────╯ ╭────── Environment Variables ───────╮ │ { │ │ 'TERM': 'xterm-256color', │ │ 'COLORTERM': 'truecolor', │ │ 'CLICOLOR': 'true', │ │ 'NO_COLOR': None, │ │ 'TERM_PROGRAM': 'tmux', │ │ 'COLUMNS': None, │ │ 'LINES': None, │ │ 'JUPYTER_COLUMNS': None, │ │ 'JUPYTER_LINES': None, │ │ 'JPY_PARENT_PID': None, │ │ 'VSCODE_VERBOSE_LOGGING': None │ │ } │ ╰────────────────────────────────────╯ platform="Darwin" rich==13.7.1 ```
github-actions[bot] commented 3 months ago

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

matkoniecz commented 3 months ago

Seems fixed on https://github.com/ipython/ipython/pull/14378 side, given that there is standard interface for this there should be no need for rich to implement support for custom interface of various projects, right?