mirror of https://github.com/python/cpython
gh-94343: Ease initialization of reprlib.Repr attributes (GH-94581)
This commit is contained in:
parent
29f86d6c28
commit
b6558d768f
|
@ -17,12 +17,31 @@ debugger and may be useful in other contexts as well.
|
||||||
This module provides a class, an instance, and a function:
|
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
|
Class which provides formatting services useful in implementing functions
|
||||||
similar to the built-in :func:`repr`; size limits for different object types
|
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.
|
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
|
.. data:: aRepr
|
||||||
|
|
||||||
|
|
|
@ -35,19 +35,23 @@ def recursive_repr(fillvalue='...'):
|
||||||
|
|
||||||
class Repr:
|
class Repr:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(
|
||||||
self.fillvalue = '...'
|
self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, maxdict=4,
|
||||||
self.maxlevel = 6
|
maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, maxlong=40,
|
||||||
self.maxtuple = 6
|
maxother=30, fillvalue='...',
|
||||||
self.maxlist = 6
|
):
|
||||||
self.maxarray = 5
|
self.maxlevel = maxlevel
|
||||||
self.maxdict = 4
|
self.maxtuple = maxtuple
|
||||||
self.maxset = 6
|
self.maxlist = maxlist
|
||||||
self.maxfrozenset = 6
|
self.maxarray = maxarray
|
||||||
self.maxdeque = 6
|
self.maxdict = maxdict
|
||||||
self.maxstring = 30
|
self.maxset = maxset
|
||||||
self.maxlong = 40
|
self.maxfrozenset = maxfrozenset
|
||||||
self.maxother = 30
|
self.maxdeque = maxdeque
|
||||||
|
self.maxstring = maxstring
|
||||||
|
self.maxlong = maxlong
|
||||||
|
self.maxother = maxother
|
||||||
|
self.fillvalue = fillvalue
|
||||||
|
|
||||||
def repr(self, x):
|
def repr(self, x):
|
||||||
return self.repr1(x, self.maxlevel)
|
return self.repr1(x, self.maxlevel)
|
||||||
|
|
|
@ -25,6 +25,28 @@ def nestedTuple(nesting):
|
||||||
|
|
||||||
class ReprTests(unittest.TestCase):
|
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):
|
def test_string(self):
|
||||||
eq = self.assertEqual
|
eq = self.assertEqual
|
||||||
eq(r("abc"), "'abc'")
|
eq(r("abc"), "'abc'")
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Allow setting the attributes of ``reprlib.Repr`` during object initialization
|
Loading…
Reference in New Issue