From 6055e2d8ef98e5487949b9c6e5d0dfce6a5b9e28 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Mon, 26 Aug 2024 16:59:16 +0100 Subject: [PATCH 1/2] fix for missing field in dataclass --- rich/pretty.py | 4 +++- tests/test_pretty.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/rich/pretty.py b/rich/pretty.py index e0c78f627..5c725c0c5 100644 --- a/rich/pretty.py +++ b/rich/pretty.py @@ -781,7 +781,9 @@ def iter_attrs() -> ( ) for last, field in loop_last( - field for field in fields(obj) if field.repr + field + for field in fields(obj) + if field.repr and hasattr(obj, field.name) ): child_node = _traverse(getattr(obj, field.name), depth=depth + 1) child_node.key_repr = field.name diff --git a/tests/test_pretty.py b/tests/test_pretty.py index 929de1cff..7ab4f8e79 100644 --- a/tests/test_pretty.py +++ b/tests/test_pretty.py @@ -734,3 +734,23 @@ def __rich_repr__(self): yield None, (1,), (1,) assert pretty_repr(Foo()) == "Foo()" + + +def test_dataclass_no_attribute() -> None: + """Regression test for https://github.com/Textualize/rich/issues/3417""" + from dataclasses import dataclass, field + + @dataclass(eq=False) + class BadDataclass: + item: int = field(init=False) + + # item is not provided + bad_data_class = BadDataclass() + + console = Console() + with console.capture() as capture: + console.print(bad_data_class) + + expected = "BadDataclass()\n" + result = capture.get() + assert result == expected From c93883011aa059b5683ae2b30d02952d21a0bc5d Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Mon, 26 Aug 2024 17:00:10 +0100 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 078a5c050..9846ee56f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed superfluous space above Markdown tables https://github.com/Textualize/rich/pull/3469 - Fixed issue with record and capture interaction https://github.com/Textualize/rich/pull/3470 - Fixed control codes breaking in `append_tokens` https://github.com/Textualize/rich/pull/3471 +- Fixed exception pretty printing a dataclass with missing fields https://github.com/Textualize/rich/pull/3472 ### Changed