Skip to content

Commit b6558d7

Browse files
authored
gh-94343: Ease initialization of reprlib.Repr attributes (GH-94581)
1 parent 29f86d6 commit b6558d7

File tree

4 files changed

+60
-14
lines changed

4 files changed

+60
-14
lines changed

Diff for: Doc/library/reprlib.rst

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,31 @@ debugger and may be useful in other contexts as well.
1717
This module provides a class, an instance, and a function:
1818

1919

20-
.. class:: Repr()
20+
.. class:: Repr(*, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, maxdict=4, \
21+
maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, maxlong=40, \
22+
maxother=30, fillvalue="...")
2123

2224
Class which provides formatting services useful in implementing functions
2325
similar to the built-in :func:`repr`; size limits for different object types
2426
are added to avoid the generation of representations which are excessively long.
2527

28+
The keyword arguments of the constructor can be used as a shortcut to set the
29+
attributes of the :class:`Repr` instance. Which means that the following
30+
initialization::
31+
32+
aRepr = reprlib.Repr(maxlevel=3)
33+
34+
Is equivalent to::
35+
36+
aRepr = reprlib.Repr()
37+
aRepr.maxlevel = 3
38+
39+
See section `Repr Objects`_ for more information about :class:`Repr`
40+
attributes.
41+
42+
.. versionchanged:: 3.12
43+
Allow attributes to be set via keyword arguments.
44+
2645

2746
.. data:: aRepr
2847

Diff for: Lib/reprlib.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,23 @@ def wrapper(self):
3535

3636
class Repr:
3737

38-
def __init__(self):
39-
self.fillvalue = '...'
40-
self.maxlevel = 6
41-
self.maxtuple = 6
42-
self.maxlist = 6
43-
self.maxarray = 5
44-
self.maxdict = 4
45-
self.maxset = 6
46-
self.maxfrozenset = 6
47-
self.maxdeque = 6
48-
self.maxstring = 30
49-
self.maxlong = 40
50-
self.maxother = 30
38+
def __init__(
39+
self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, maxdict=4,
40+
maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, maxlong=40,
41+
maxother=30, fillvalue='...',
42+
):
43+
self.maxlevel = maxlevel
44+
self.maxtuple = maxtuple
45+
self.maxlist = maxlist
46+
self.maxarray = maxarray
47+
self.maxdict = maxdict
48+
self.maxset = maxset
49+
self.maxfrozenset = maxfrozenset
50+
self.maxdeque = maxdeque
51+
self.maxstring = maxstring
52+
self.maxlong = maxlong
53+
self.maxother = maxother
54+
self.fillvalue = fillvalue
5155

5256
def repr(self, x):
5357
return self.repr1(x, self.maxlevel)

Diff for: Lib/test/test_reprlib.py

+22
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@ def nestedTuple(nesting):
2525

2626
class ReprTests(unittest.TestCase):
2727

28+
def test_init_kwargs(self):
29+
example_kwargs = {
30+
"maxlevel": 101,
31+
"maxtuple": 102,
32+
"maxlist": 103,
33+
"maxarray": 104,
34+
"maxdict": 105,
35+
"maxset": 106,
36+
"maxfrozenset": 107,
37+
"maxdeque": 108,
38+
"maxstring": 109,
39+
"maxlong": 110,
40+
"maxother": 111,
41+
"fillvalue": "x" * 112,
42+
}
43+
r1 = Repr()
44+
for attr, val in example_kwargs.items():
45+
setattr(r1, attr, val)
46+
r2 = Repr(**example_kwargs)
47+
for attr in example_kwargs:
48+
self.assertEqual(getattr(r1, attr), getattr(r2, attr), msg=attr)
49+
2850
def test_string(self):
2951
eq = self.assertEqual
3052
eq(r("abc"), "'abc'")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow setting the attributes of ``reprlib.Repr`` during object initialization

0 commit comments

Comments
 (0)