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] __rich__ is ignored when displaying dictionary of objects #3618

Closed
2 tasks done
matkoniecz opened this issue Jan 24, 2025 · 3 comments
Closed
2 tasks done

[BUG] __rich__ is ignored when displaying dictionary of objects #3618

matkoniecz opened this issue Jan 24, 2025 · 3 comments

Comments

@matkoniecz
Copy link

matkoniecz commented Jan 24, 2025

Describe the bug

I expected __rich__ to be used for dict of objects. It is not

a minimal code example:

import rich

class Minimal():
    def __init__(self):
        pass
    def __str__(self):
        return "content"
    def __rich__(self):
        return self.__str__()

if __name__ == "__main__":
    test = Minimal()
    print(test) # works
    rich.print(test) # works
    print({'a': test}) # fails, as expected
    rich.print({'a': test}) # fails unexpectedly

Image

Platform

Click to expand

What platform (Win/Linux/Mac) are you running on? What terminal software are you using?

linux, qterminal

I may ask you to copy and paste the output of the following commands. It may save some time if you do it now.

If you're using Rich in a terminal:

python -m rich.diagnose
pip freeze | grep rich
╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=189 ColorSystem.EIGHT_BIT>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = '256'                                                         │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 55                                                            │
│    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=189, height=55),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=189,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=55,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=189, height=55)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 189                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ {                                  │
│     'TERM': 'xterm-256color',      │
│     'COLORTERM': None,             │
│     'CLICOLOR': None,              │
│     'NO_COLOR': None,              │
│     'TERM_PROGRAM': None,          │
│     'COLUMNS': None,               │
│     'LINES': None,                 │
│     'JUPYTER_COLUMNS': None,       │
│     'JUPYTER_LINES': None,         │
│     'JPY_PARENT_PID': None,        │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
╰────────────────────────────────────╯
platform="Linux"
rich==13.9.4
@TomJGooding
Copy link
Contributor

Perhaps @willmcgugan will correct me on this, but I think you're expecting too much from Rich here.

The __rich__ method only works when printing the object directly. When printing dicts and other containers that contain the object, Rich will fall back to the default __repr__ method.

You probably need the Rich repr protocol instead: https://rich.readthedocs.io/en/stable/pretty.html#rich-repr-protocol)

@Textualize Textualize deleted a comment from github-actions bot Jan 24, 2025
@willmcgugan
Copy link
Collaborator

Tom is exactly right. If you print a collection, Rich will render it as a data structure.

Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

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

No branches or pull requests

3 participants