Skip to content

Commit 0394ebf

Browse files
committed
saferepr: Use an __init__ instead of setting attributes after construction
This will be easier to type-check, and also somewhat clearer.
1 parent 0225be5 commit 0394ebf

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/_pytest/_io/saferepr.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class SafeRepr(reprlib.Repr):
2222
and includes information on exceptions raised during the call.
2323
"""
2424

25+
def __init__(self, maxsize):
26+
super().__init__()
27+
self.maxstring = maxsize
28+
self.maxsize = maxsize
29+
self.maxother = 160
30+
2531
def repr(self, x):
2632
return self._callhelper(reprlib.Repr.repr, self, x)
2733

@@ -52,9 +58,4 @@ def saferepr(obj, maxsize=240):
5258
care to never raise exceptions itself. This function is a wrapper
5359
around the Repr/reprlib functionality of the standard 2.6 lib.
5460
"""
55-
# review exception handling
56-
srepr = SafeRepr()
57-
srepr.maxstring = maxsize
58-
srepr.maxsize = maxsize
59-
srepr.maxother = 160
60-
return srepr.repr(obj)
61+
return SafeRepr(maxsize).repr(obj)

testing/io/test_saferepr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class BrokenReprException(Exception):
4848
def test_big_repr():
4949
from _pytest._io.saferepr import SafeRepr
5050

51-
assert len(saferepr(range(1000))) <= len("[" + SafeRepr().maxlist * "1000" + "]")
51+
assert len(saferepr(range(1000))) <= len("[" + SafeRepr(0).maxlist * "1000" + "]")
5252

5353

5454
def test_repr_on_newstyle():

0 commit comments

Comments
 (0)