From 900abf8ce300a0e5c3f1e3536faab7c589755b28 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 3 Nov 2026 21:25:08 -0500 Subject: [PATCH 1/2] Add a __repr__ to reprlib.Repr --- Doc/library/reprlib.rst | 4 ++++ Lib/reprlib.py | 4 ++++ Lib/test/test_reprlib.py | 8 ++++++++ 3 files changed, 16 insertions(+) diff --git a/Doc/library/reprlib.rst b/Doc/library/reprlib.rst index d269d8bbaa55da5..75d0df481c79b41 100644 --- a/Doc/library/reprlib.rst +++ b/Doc/library/reprlib.rst @@ -40,6 +40,10 @@ This module provides a class, an instance, and a function: .. versionchanged:: 3.12 Allow attributes to be set via keyword arguments. + .. versionchanged:: next + :class:`!Repr` instances now have a string representation that includes + their attribute values. + .. data:: aRepr diff --git a/Lib/reprlib.py b/Lib/reprlib.py index ab18247682b69aa..8e3dff2f1ba3455 100644 --- a/Lib/reprlib.py +++ b/Lib/reprlib.py @@ -67,6 +67,10 @@ def __init__( self.fillvalue = fillvalue self.indent = indent + def __repr__(self): + args = ', '.join(f'{key}={value!r}' for key, value in self.__dict__.items()) + return f'{type(self).__name__}({args})' + def repr(self, x): return self.repr1(x, self.maxlevel) diff --git a/Lib/test/test_reprlib.py b/Lib/test/test_reprlib.py index 5a95a05c6496a29..ecfd199fde45315 100644 --- a/Lib/test/test_reprlib.py +++ b/Lib/test/test_reprlib.py @@ -27,6 +27,14 @@ def nestedTuple(nesting): class ReprTests(unittest.TestCase): + def test_repr(self): + self.assertEqual( + repr(Repr(maxlevel=3, fillvalue='***', indent='\t')), + "Repr(maxlevel=3, maxtuple=6, maxlist=6, maxarray=5, maxdict=4, " + "maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, maxlong=40, " + "maxother=30, fillvalue='***', indent='\\t')", + ) + def test_init_kwargs(self): example_kwargs = { "maxlevel": 101, From 1982ce62edb43fd093cb67a73f3631fabe1406ef Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 3 Nov 2026 21:26:11 -0500 Subject: [PATCH 2/2] Add blurb. --- .../next/Library/2026-11-03-21-26-09.gh-issue-161295.IJqoJM.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-03-21-26-09.gh-issue-161295.IJqoJM.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-03-21-26-09.gh-issue-161295.IJqoJM.rst b/Misc/NEWS.d/next/Library/2026-11-03-21-26-09.gh-issue-161295.IJqoJM.rst new file mode 100644 index 000000000000000..f2ede87899e9406 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-03-21-26-09.gh-issue-161295.IJqoJM.rst @@ -0,0 +1,2 @@ +:class:`reprlib.Repr` objects now have a meaningful :meth:`~object.__repr__` +describing the instance attributes.