Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ExceptionInfo.__repr__ #5934

merged 1 commit into from Oct 9, 2019
Changes from all commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.


Just for now

@@ -0,0 +1 @@
``repr`` of ``ExceptionInfo`` objects has been improved to honor the ``__repr__`` method of the underlying exception.

This comment has been minimized.

Copy link

asottile Nov 20, 2019


oops, this file ended up in the wrong directory

@@ -507,7 +507,9 @@ def traceback(self, value: Traceback) -> None:
def __repr__(self) -> str:
if self._excinfo is None:
return "<ExceptionInfo for raises contextmanager>"
return "<ExceptionInfo %s tblen=%d>" % (self.typename, len(self.traceback))
return "<{} {} tblen={}>".format(
self.__class__.__name__, saferepr(self._excinfo[1]), len(self.traceback)

def exconly(self, tryshort: bool = False) -> str:
""" return the exception as a string
@@ -316,8 +316,19 @@ def test_excinfo_exconly():

def test_excinfo_repr_str():
excinfo = pytest.raises(ValueError, h)
assert repr(excinfo) == "<ExceptionInfo ValueError tblen=4>"
assert str(excinfo) == "<ExceptionInfo ValueError tblen=4>"
assert repr(excinfo) == "<ExceptionInfo ValueError() tblen=4>"
assert str(excinfo) == "<ExceptionInfo ValueError() tblen=4>"

class CustomException(Exception):
def __repr__(self):
return "custom_repr"

def raises():
raise CustomException()

excinfo = pytest.raises(CustomException, raises)
assert repr(excinfo) == "<ExceptionInfo custom_repr tblen=2>"
assert str(excinfo) == "<ExceptionInfo custom_repr tblen=2>"

def test_excinfo_for_later():
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.