summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfinefoot <33361833+finefoot@users.noreply.github.com>2022-07-07 14:55:33 (GMT)
committerGitHub <noreply@github.com>2022-07-07 14:55:33 (GMT)
commitb6558d768f19584ad724be23030603280f9e6361 (patch)
tree30f7a50014e3deff7e1db1b69ffc2dacd4869b96
parent29f86d6c28fa9ed39e42678776d6005eaf5090c8 (diff)
downloadcpython-b6558d768f19584ad724be23030603280f9e6361.zip
cpython-b6558d768f19584ad724be23030603280f9e6361.tar.gz
cpython-b6558d768f19584ad724be23030603280f9e6361.tar.bz2
gh-94343: Ease initialization of reprlib.Repr attributes (GH-94581)
-rw-r--r--Doc/library/reprlib.rst21
-rw-r--r--Lib/reprlib.py30
-rw-r--r--Lib/test/test_reprlib.py22
-rw-r--r--Misc/NEWS.d/next/Library/2022-07-05-17-22-00.gh-issue-94343.kf4H5r.rst1
4 files changed, 60 insertions, 14 deletions
diff --git a/Doc/library/reprlib.rst b/Doc/library/reprlib.rst
index 4b37c5b..bb94b60 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 f3518df..c33b4da 100644
--- a/Lib/reprlib.py
+++ b/Lib/reprlib.py
@@ -35,19 +35,23 @@ def recursive_repr(fillvalue='...'):
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 aa32639..5119511 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 0000000..f666c2b
--- /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