Textualize / rich

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

[REQUEST] Make classes pretty printable via __rich_repr__ #3118

Open jankatins opened 1 year ago

jankatins commented 1 year ago

How would you improve Rich?

Currently a class will not use __rich_repr__() as the code has it hard coded to ignore classes:

https://github.com/Textualize/rich/blob/720800e6930d85ad027b1e9bd0cbb96b5e994ce3/rich/pretty.py#L639-L643

It would be nice if there is a way to define a rich repr to print the class (e.g. using a metaclass). E.g. by marking the function ala __rich_repr__.use_on_class = True or using a class specific protocol __rich_repr_class__(cls).

What problem does it solve for you?

We use classes for defining some data structure (mostly because using the python editor really convenient). It would be nice to pretty print the data structure. I can imagine that pretty printing pydantic/sqlalachemy classes have a similar problem.

Example:

class Metadata: pass
class Attribute: pass
class BaseObject(): pass

class SomeDefinition(BaseObject):
    __metadata__ = Metadata(...)
   some_attribute = Attribute(...)
   some_other_attribute = Attribute(...)

With some hackery it is actually possible:

github-actions[bot] commented 1 year 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

willmcgugan commented 1 year ago

When you say "print the class" what kind of output are you expecting?

jankatins commented 1 year ago

Good question: in our case, I want to treat the class as every other object and print the class variables.

Example:

class Whatever:
    x = Attribute(...)
    y = Attribute(...)

    @classmethod
    def __rich_class_repr__(cls):
        yield "x", cls.x
        yield "y", cls.y

would print more or less as if would have instantiated the class and used the regular __rich_repr__(self) protocol.