Skip to content

Commit 9f5c356

Browse files
authored
Remove ExceptionInfo.__str__, falling back to __repr__ (pytest-dev#5413)
Remove ExceptionInfo.__str__, falling back to __repr__
2 parents 042a10f + 65c2a81 commit 9f5c356

File tree

3 files changed

+5
-18
lines changed

3 files changed

+5
-18
lines changed

changelog/5412.removal.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``ExceptionInfo`` objects (returned by ``pytest.raises``) now have the same ``str`` representation as ``repr``, which
2+
avoids some confusion when users use ``print(e)`` to inspect the object.

src/_pytest/_code/code.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -534,13 +534,6 @@ def getrepr(
534534
)
535535
return fmt.repr_excinfo(self)
536536

537-
def __str__(self):
538-
if self._excinfo is None:
539-
return repr(self)
540-
entry = self.traceback[-1]
541-
loc = ReprFileLocation(entry.path, entry.lineno + 1, self.exconly())
542-
return str(loc)
543-
544537
def match(self, regexp):
545538
"""
546539
Check whether the regular expression 'regexp' is found in the string

testing/code/test_excinfo.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,10 @@ def test_excinfo_exconly():
333333
assert msg.endswith("world")
334334

335335

336-
def test_excinfo_repr():
336+
def test_excinfo_repr_str():
337337
excinfo = pytest.raises(ValueError, h)
338-
s = repr(excinfo)
339-
assert s == "<ExceptionInfo ValueError tblen=4>"
340-
341-
342-
def test_excinfo_str():
343-
excinfo = pytest.raises(ValueError, h)
344-
s = str(excinfo)
345-
assert s.startswith(__file__[:-9]) # pyc file and $py.class
346-
assert s.endswith("ValueError")
347-
assert len(s.split(":")) >= 3 # on windows it's 4
338+
assert repr(excinfo) == "<ExceptionInfo ValueError tblen=4>"
339+
assert str(excinfo) == "<ExceptionInfo ValueError tblen=4>"
348340

349341

350342
def test_excinfo_for_later():

0 commit comments

Comments
 (0)