diff --git a/Doc/library/reprlib.rst b/Doc/library/reprlib.rst index d269d8bbaa55da..75d0df481c79b4 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 ab18247682b69a..8e3dff2f1ba345 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 5a95a05c6496a2..ecfd199fde4531 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, 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 00000000000000..f2ede87899e940 --- /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.