Skip to content
Draft

Bug fix #4017

Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion rich/repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def auto_rich_repr(self: Type[T]) -> Result:
signature = inspect.signature(self.__init__)
for name, param in signature.parameters.items():
if param.kind == param.POSITIONAL_ONLY:
yield getattr(self, name)
yield None, getattr(self, name)
elif param.kind in (
param.POSITIONAL_OR_KEYWORD,
param.KEYWORD_ONLY,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import attr
import pytest

import rich.repr
from rich.console import Console
from rich.measure import Measurement
from rich.pretty import Node, Pretty, _ipy_display_hook, install, pprint, pretty_repr
Expand Down Expand Up @@ -739,6 +740,20 @@ def __rich_repr__(self):
assert pretty_repr(Foo()) == "Foo()"


def test_auto_tuple_positional_only_repr() -> None:
class ClassA:
pass

@rich.repr.auto
class PosOnlyTuple:
def __init__(self, pair, /):
self.pair = pair

result = pretty_repr(PosOnlyTuple((ClassA(), ClassA())))
assert result.startswith("PosOnlyTuple(")
assert "ClassA object" in result


def test_dataclass_no_attribute() -> None:
"""Regression test for https://github.com/Textualize/rich/issues/3417"""
from dataclasses import dataclass, field
Expand Down
21 changes: 21 additions & 0 deletions tests/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,27 @@ def __init__(self, foo, /):
assert repr(p) == "PosOnly(1)"


def test_rich_repr_positional_only_tuple() -> None:
class ClassA:
pass

_locals = locals().copy()
exec(
"""\
@rich.repr.auto
class PosOnlyTuple:
def __init__(self, pair, /):
self.pair = pair
""",
globals(),
_locals,
)
p = _locals["PosOnlyTuple"]((ClassA(), ClassA()))
result = repr(p)
assert result.startswith("PosOnlyTuple((")
assert "ClassA object" in result


def test_rich_angular() -> None:
assert (repr(Bar("hello"))) == "<Bar 'hello' 'hello' egg=1>"
assert (repr(Bar("hello", bar=3))) == "<Bar 'hello' 'hello' bar=3 egg=1>"
Expand Down