From 58f948d9761f9cf85ab2925780d353dde678350d Mon Sep 17 00:00:00 2001 From: Collin Heist Date: Sat, 6 Jul 2024 17:32:40 -0600 Subject: [PATCH 1/3] Handle dataclasses with unitialized fields in pretty printing Fix for https://github.com/Textualize/rich/issues/3417 --- rich/pretty.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rich/pretty.py b/rich/pretty.py index fa340212e..e7c1d82fe 100644 --- a/rich/pretty.py +++ b/rich/pretty.py @@ -779,7 +779,7 @@ def iter_attrs() -> ( for last, field in loop_last( field for field in fields(obj) if field.repr ): - child_node = _traverse(getattr(obj, field.name), depth=depth + 1) + child_node = _traverse(getattr(obj, field.name, field.default), depth=depth + 1) child_node.key_repr = field.name child_node.last = last child_node.key_separator = "=" From d2b5ea2c8d88b3ba294180978129ec9b2f7c721b Mon Sep 17 00:00:00 2001 From: Collin Heist Date: Sat, 6 Jul 2024 17:37:52 -0600 Subject: [PATCH 2/3] Update Changelog and Contributors list --- CHANGELOG.md | 1 + CONTRIBUTORS.md | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1a042249..2f199ff5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Progress track thread is now a daemon thread https://github.com/Textualize/rich/pull/3402 - Fixed cached hash preservation upon clearing meta and links https://github.com/Textualize/rich/issues/2942 - Fixed overriding the `background_color` of `Syntax` not including padding https://github.com/Textualize/rich/issues/3295 +- Fixed pretty printing of dataclasses with uninitialized fields https://github.com/Textualize/rich/issues/3417 ### Changed diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index edacc5885..a466e3497 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -85,3 +85,4 @@ The following people have contributed to the development of Rich: - [Pierro](https://github.com/xpierroz) - [Bernhard Wagner](https://github.com/bwagner) - [Aaron Beaudoin](https://github.com/AaronBeaudoin) +- [Collin Heist](https://github.com/CollinHeist/) From 7037006dc4d6889fa6d12f7bbede2190e68856e3 Mon Sep 17 00:00:00 2001 From: Collin Heist Date: Mon, 26 Aug 2024 09:57:21 -0600 Subject: [PATCH 3/3] Skip uninitialized dataclass attributes in pretty printing --- rich/pretty.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rich/pretty.py b/rich/pretty.py index e7c1d82fe..1660a63b2 100644 --- a/rich/pretty.py +++ b/rich/pretty.py @@ -779,7 +779,11 @@ def iter_attrs() -> ( for last, field in loop_last( field for field in fields(obj) if field.repr ): - child_node = _traverse(getattr(obj, field.name, field.default), depth=depth + 1) + try: + child_node = _traverse(getattr(obj, field.name), depth=depth + 1) + except AttributeError: + continue + child_node.key_repr = field.name child_node.last = last child_node.key_separator = "="