diff --git a/Doc/library/reprlib.rst b/Doc/library/reprlib.rst index 4b37c5ba60f..bb94b6089ad 100644 --- a/Doc/library/reprlib.rst +++ b/Doc/library/reprlib.rst @@ -17,12 +17,31 @@ debugger and may be useful in other contexts as well. This module provides a class, an instance, and a function: -.. class:: Repr() +.. class:: Repr(*, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, maxdict=4, \ + maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, maxlong=40, \ + maxother=30, fillvalue="...") Class which provides formatting services useful in implementing functions similar to the built-in :func:`repr`; size limits for different object types are added to avoid the generation of representations which are excessively long. + The keyword arguments of the constructor can be used as a shortcut to set the + attributes of the :class:`Repr` instance. Which means that the following + initialization:: + + aRepr = reprlib.Repr(maxlevel=3) + + Is equivalent to:: + + aRepr = reprlib.Repr() + aRepr.maxlevel = 3 + + See section `Repr Objects`_ for more information about :class:`Repr` + attributes. + + .. versionchanged:: 3.12 + Allow attributes to be set via keyword arguments. + .. data:: aRepr diff --git a/Lib/reprlib.py b/Lib/reprlib.py index f3518df105e..c33b4da166f 100644 --- a/Lib/reprlib.py +++ b/Lib/reprlib.py @@ -35,19 +35,23 @@ def wrapper(self): class Repr: - def __init__(self): - self.fillvalue = '...' - self.maxlevel = 6 - self.maxtuple = 6 - self.maxlist = 6 - self.maxarray = 5 - self.maxdict = 4 - self.maxset = 6 - self.maxfrozenset = 6 - self.maxdeque = 6 - self.maxstring = 30 - self.maxlong = 40 - self.maxother = 30 + def __init__( + self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, maxdict=4, + maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, maxlong=40, + maxother=30, fillvalue='...', + ): + self.maxlevel = maxlevel + self.maxtuple = maxtuple + self.maxlist = maxlist + self.maxarray = maxarray + self.maxdict = maxdict + self.maxset = maxset + self.maxfrozenset = maxfrozenset + self.maxdeque = maxdeque + self.maxstring = maxstring + self.maxlong = maxlong + self.maxother = maxother + self.fillvalue = fillvalue 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 aa326399ab2..5119511f5d6 100644 --- a/Lib/test/test_reprlib.py +++ b/Lib/test/test_reprlib.py @@ -25,6 +25,28 @@ def nestedTuple(nesting): class ReprTests(unittest.TestCase): + def test_init_kwargs(self): + example_kwargs = { + "maxlevel": 101, + "maxtuple": 102, + "maxlist": 103, + "maxarray": 104, + "maxdict": 105, + "maxset": 106, + "maxfrozenset": 107, + "maxdeque": 108, + "maxstring": 109, + "maxlong": 110, + "maxother": 111, + "fillvalue": "x" * 112, + } + r1 = Repr() + for attr, val in example_kwargs.items(): + setattr(r1, attr, val) + r2 = Repr(**example_kwargs) + for attr in example_kwargs: + self.assertEqual(getattr(r1, attr), getattr(r2, attr), msg=attr) + def test_string(self): eq = self.assertEqual eq(r("abc"), "'abc'") diff --git a/Misc/NEWS.d/next/Library/2022-07-05-17-22-00.gh-issue-94343.kf4H5r.rst b/Misc/NEWS.d/next/Library/2022-07-05-17-22-00.gh-issue-94343.kf4H5r.rst new file mode 100644 index 00000000000..f666c2b0ec1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-07-05-17-22-00.gh-issue-94343.kf4H5r.rst @@ -0,0 +1 @@ +Allow setting the attributes of ``reprlib.Repr`` during object initialization