Support PEP-415's Exception.__suppress_context__ #2631
PEP-415 states that
The current algorithm in
if e.__cause__ is not None: # Code to chain the cause. elif e.__context__ is not None: # Code to chain the context.
which means that pytest always chains the exception, assuming that
By comparison, the algorithm in
if e.__cause__ is not None: # Code to chain the cause. elif (e.__context__ is not None and not e.__suppress_context__): # Code to chain the context.
Here's a test that has a
def test_raise_from_none(): try: raise ValueError() except Exception: raise AttributeError() from None
This is the test output (with the chained exception traceback) that results when running against the pytest feature branch (commit 768edde):
But running the same code in a terminal produces this shorter, non-chained traceback:
And this is what the pytest output should look like:
The text was updated successfully, but these errors were encountered:
PEP-415 states that `exception.__context__` should be suppressed in traceback outputs, if `exception.__suppress_context__` is `True`. Now if a ``raise exception from None`` is caught by pytest, pytest will no longer chain the context in the test report. The algorithm in `FormattedExcinfo` now better matches the one in `traceback.TracebackException`. `Exception.__suppress_context__` is available in all of the versions of Python 3 that are supported by pytest. Fixes pytest-dev#2631.