diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d355b82b..10dcf8b3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Parse ANSI escape sequences in pretty repr https://github.com/Textualize/rich/pull/2470 - Add support for `FORCE_COLOR` env var https://github.com/Textualize/rich/pull/2449 - Allow a `max_depth` argument to be passed to the `install()` hook https://github.com/Textualize/rich/issues/2486 +- Document using `None` as name in `__rich_repr__` for tuple posotional args https://github.com/Textualize/rich/pull/2379 ### Fixed diff --git a/docs/source/pretty.rst b/docs/source/pretty.rst index 7a050b943..29c0110a8 100644 --- a/docs/source/pretty.rst +++ b/docs/source/pretty.rst @@ -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 diff --git a/tests/test_pretty.py b/tests/test_pretty.py index 33f621134..163a9d92b 100644 --- a/tests/test_pretty.py +++ b/tests/test_pretty.py @@ -589,3 +589,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()"