Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with custom classes (fixes #2875) #3006

Merged
merged 2 commits into from
Jul 29, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix for escaping strings with a trailing backslash https://github.com/Textualize/rich/issues/2987
- Fixed exception in Markdown with partial table https://github.com/Textualize/rich/issues/3053
- Fixed the HTML export template so that the `<html>` tag comes before the `<head>` tag https://github.com/Textualize/rich/issues/3021
- Fixed issue with custom classes overwriting `__eq__` https://github.com/Textualize/rich/issues/2875

### Added

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The following people have contributed to the development of Rich:
- [Ed Davis](https://github.com/davised)
- [Pete Davison](https://github.com/pd93)
- [James Estevez](https://github.com/jstvz)
- [Aryaz Eghbali](https://github.com/AryazE)
- [Oleksis Fraga](https://github.com/oleksis)
- [Andy Gimblett](https://github.com/gimbo)
- [Michał Górny](https://github.com/mgorny)
Expand Down
2 changes: 1 addition & 1 deletion rich/repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def auto_rich_repr(self: Type[T]) -> Result:
param.POSITIONAL_OR_KEYWORD,
param.KEYWORD_ONLY,
):
if param.default == param.empty:
if param.default is param.empty:
yield getattr(self, param.name)
else:
yield param.name, getattr(self, param.name), param.default
Expand Down
40 changes: 40 additions & 0 deletions tests/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import rich.repr
from rich.console import Console

from inspect import Parameter

skip_py37 = pytest.mark.skipif(
sys.version_info.minor == 7 and sys.version_info.major == 3,
reason="rendered differently on py3.7",
Expand Down Expand Up @@ -61,6 +63,38 @@ def __rich_repr__(self):
__rich_repr__.angular = True


class StupidClass:
def __init__(self, a):
self.a = a

def __eq__(self, other) -> bool:
if other is Parameter.empty:
return True
try:
return self.a == other.a
except Exception:
return False

def __ne__(self, other: object) -> bool:
return not self.__eq__(other)


class NotStupid:
pass


@rich.repr.auto
class Bird:
def __init__(
self, name, eats, fly=True, another=StupidClass(2), extinct=NotStupid()
):
self.name = name
self.eats = eats
self.fly = fly
self.another = another
self.extinct = extinct


def test_rich_repr() -> None:
assert (repr(Foo("hello"))) == "Foo('hello', 'hello', egg=1)"
assert (repr(Foo("hello", bar=3))) == "Foo('hello', 'hello', bar=3, egg=1)"
Expand Down Expand Up @@ -90,6 +124,12 @@ def test_rich_angular() -> None:

def test_rich_repr_auto() -> None:
assert repr(Egg("hello", egg=2)) == "Egg('hello', egg=2)"
stupid_class = StupidClass(9)
not_stupid = NotStupid()
assert (
repr(Bird("penguin", ["fish"], another=stupid_class, extinct=not_stupid))
== f"Bird('penguin', ['fish'], another={repr(stupid_class)}, extinct={repr(not_stupid)})"
)


def test_rich_repr_auto_angular() -> None:
Expand Down