]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-94343: Ease initialization of reprlib.Repr attributes (GH-94581)
authorfinefoot <33361833+finefoot@users.noreply.github.com>
Thu, 7 Jul 2022 14:55:33 +0000 (16:55 +0200)
committerGitHub <noreply@github.com>
Thu, 7 Jul 2022 14:55:33 +0000 (09:55 -0500)
Doc/library/reprlib.rst
Lib/reprlib.py
Lib/test/test_reprlib.py
Misc/NEWS.d/next/Library/2022-07-05-17-22-00.gh-issue-94343.kf4H5r.rst [new file with mode: 0644]

index 4b37c5ba60f4e62c8af00fc9b309d5e86868b0f2..bb94b6089ad3d517bf2d0d795a225b4e3ad34fea 100644 (file)
@@ -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
 
index f3518df105e418a995502b39c669c03b3d013ca7..c33b4da166fac336f555414f591f54b0b3ea4895 100644 (file)
@@ -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)
index aa326399ab22471fe343208d41c674bd687714de..5119511f5d68958aece86cd5d530cbf948ec2f24 100644 (file)
@@ -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 (file)
index 0000000..f666c2b
--- /dev/null
@@ -0,0 +1 @@
+Allow setting the attributes of ``reprlib.Repr`` during object initialization