Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix edges used in first row of tables when `show_header=False` https://github.com/Textualize/rich/pull/2330
- Fix interaction between `Capture` contexts and `Console(record=True)` https://github.com/Textualize/rich/pull/2343
- Fixed hash issue in Styles class https://github.com/Textualize/rich/pull/2346
- Document using `None` as name in `__rich_repr__` for tuple posotional args https://github.com/Textualize/rich/pull/2379

### Changed

Expand Down
2 changes: 2 additions & 0 deletions docs/source/pretty.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ Each tuple specifies an element in the output.
- ``yield name, value`` will generate a keyword argument.
- ``yield name, value, default`` will generate a keyword argument *if* ``value`` is not equal to ``default``.

If you use ``None`` as the ``name``, then it will be treated as a positional argument as well, in order to support having ``tuple`` positional arguments.

You can also tell Rich to generate the *angular bracket* style of repr, which tend to be used where there is no easy way to recreate the object's constructor. To do this set the function attribute ``"angular"`` to ``True`` immediately after your ``__rich_repr__`` method. For example::

__rich_repr__.angular = True
Expand Down
24 changes: 24 additions & 0 deletions tests/test_pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,27 @@ def test_measure_pretty():

measurement = console.measure(pretty)
assert measurement == Measurement(12, 12)


def test_tuple_rich_repr():
"""
Test that can use None as key to have tuple positional values.
"""

class Foo:
def __rich_repr__(self):
yield None, (1,)

assert pretty_repr(Foo()) == "Foo((1,))"


def test_tuple_rich_repr_default():
"""
Test that can use None as key to have tuple positional values and with a default.
"""

class Foo:
def __rich_repr__(self):
yield None, (1,), (1,)

assert pretty_repr(Foo()) == "Foo()"