Skip to content

Commit

Permalink
Add typing
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Aug 5, 2021
1 parent 52d159a commit dd53faa
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/source/pretty.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,29 @@ This will change the output of the Rich repr example to the following::

Note that you can add ``__rich_repr__`` methods to third-party libraries *without* including Rich as a dependency. If Rich is not installed, then nothing will break. Hopefully more third-party libraries will adopt Rich repr methods in the future.

Typing
~~~~~~

If you want to type the Rich repr method you can import and return the type `rich.repr.Result`, which will help catch logical errors::


import rich.repr


class Bird:
def __init__(self, name, eats=None, fly=True, extinct=False):
self.name = name
self.eats = list(eats) if eats else []
self.fly = fly
self.extinct = extinct

def __rich_repr__(self) -> rich.repr.Result:
yield self.name
yield "eats", self.eats
yield "fly", self.fly, True
yield "extinct", self.extinct, False

Automatic Rich Repr
~~~~~~~~~~~~~~~~~~~

Expand Down

0 comments on commit dd53faa

Please sign in to comment.