From ef8f23186eacbcc51777a948897ebb646978dd8c Mon Sep 17 00:00:00 2001 From: Jonathan Helmus Date: Fri, 16 Aug 2024 12:10:23 -0500 Subject: [PATCH 1/2] Handle dataclasses with Python 3.13 default repr The reprlib module is used for the default __repr__ method in dataclasses starting in Python 3.13. Allow this module to also be an indication that a dataclass has the default __repr__. closes #3368 --- rich/pretty.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rich/pretty.py b/rich/pretty.py index fa340212e..e0c78f627 100644 --- a/rich/pretty.py +++ b/rich/pretty.py @@ -3,6 +3,7 @@ import dataclasses import inspect import os +import reprlib import sys from array import array from collections import Counter, UserDict, UserList, defaultdict, deque @@ -78,7 +79,10 @@ def _is_dataclass_repr(obj: object) -> bool: # Digging in to a lot of internals here # Catching all exceptions in case something is missing on a non CPython implementation try: - return obj.__repr__.__code__.co_filename == dataclasses.__file__ + return obj.__repr__.__code__.co_filename in ( + dataclasses.__file__, + reprlib.__file__, + ) except Exception: # pragma: no coverage return False From 848fdde0790b1b4ca8d81e5b14a8db537650e67c Mon Sep 17 00:00:00 2001 From: Jonathan Helmus Date: Fri, 16 Aug 2024 12:50:58 -0500 Subject: [PATCH 2/2] 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..5c74c4d35 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 a default repr in Python 3.13 https://github.com/Textualize/rich/pull/3455 ### Changed diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index edacc5885..7b95e1484 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) +- [Jonathan Helmus](https://github.com/jjhelmus)