mirror of
https://github.com/python/cpython
synced 2024-09-05 08:17:45 +00:00
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:
|
||||
|
||||
|
||||
.. 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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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'")
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Allow setting the attributes of ``reprlib.Repr`` during object initialization
|
Loading…
Reference in a new issue