Skip to content

Filter internal and hidden traceback frames for custom items - #15430

Open
RonnyPfannschmidt wants to merge 4 commits into
mainfrom
claude/project-thread-5ciyai
Open

RonnyPfannschmidt wants to merge 4 commits into
mainfrom
claude/project-thread-5ciyai

Conversation

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

Requested by Ronny · project thread

🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.

Node._traceback_filter was a no-op, so custom Item failures showed the runner/pluggy/plugin-wrapper frames and __tracebackhide__ frames. It now does the generic part of what Function does: drop pytest/pluggy frames (falling back to the full traceback if that empties it), then hidden frames; --full-trace is unchanged. The test covers a custom item failing in setup and runtest, with ValueError and pytest.fail, with and without --full-trace.

Closes #7692. Alternative to #15256, which only handles __tracebackhide__.


Generated by Claude Code

Node._traceback_filter returned the traceback unchanged, so failures in
custom Item subclasses showed the runner, pluggy and plugin wrapper
frames as well as frames marked with __tracebackhide__. Apply the same
generic filtering Function falls back to: drop pytest/pluggy frames
(keeping the original if that empties it), then hidden frames.
--full-trace still shows everything.

Closes #7692.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014GZsRDDitRunjszt4LxLHz
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 22, 2026
@RonnyPfannschmidt RonnyPfannschmidt self-assigned this Nov 22, 2026
Comment thread src/_pytest/nodes.py Outdated
def _traceback_filter(self, excinfo: ExceptionInfo[BaseException]) -> Traceback:
return excinfo.traceback
traceback = excinfo.traceback
ntraceback = traceback.filter(filter_traceback)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generated with Claude assistance

This removes internal and generated frames everywhere, including below the item's own code. For example:

  • runtest calls a pytest helper that raises from inside _pytest without __tracebackhide__, or
  • runtest exec()s user code compiled with a <...> filename (the notebook and markdown runner plugins do this).

In both cases the frame that raised is dropped and the traceback ends at the caller. A test function in the same situation keeps that frame, because Function only cuts the frames above the test.

Could we cut the leading internal frames instead? Something like:

def _traceback_filter(self, excinfo):
    traceback = excinfo.traceback
    for i, entry in enumerate(traceback):
        if filter_traceback(entry):
            traceback = traceback[i:]
            break
    return traceback.filter(excinfo)

(If nothing matches, this keeps the full traceback, which also covers the fallback case.)

Related: ExceptionInfo._getreprcrash ignores tbfilter. It picks the last frame that isn't __tracebackhide__, even when that frame is in _pytest or is generated code. With this filter, the short summary line, the path:line: Error line and the junitxml message can point at a frame the displayed traceback no longer shows. A cut avoids this, because it never drops the innermost frames.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.

Agreed, the filter dropped frames below the item. Done in 657e4aa, along the lines you sketched: TracebackEntry.isinternal()/isgenerated() (with filter_traceback as a thin wrapper) and Traceback.cut_internal(); Function's innermost fallback and the _obj-less / path-less Collector cases now reuse Node._traceback_filter.

One addition: Node._traceback_filter first cuts at the node's own method frame (the first entry whose self is the node, like Function cuts at the test), and only falls back to cut_internal() when there is none (e.g. an error in a conftest hook). The leading cut alone stops at the first third-party frame, so with a plugin pytest_runtest_call wrapper on the stack (hypothesis has one) the skipping.py/runner.py frames below it came back.


Generated by Claude Code

Comment thread testing/test_nodes.py
@pytest.mark.parametrize(
"failure", ["raise ValueError('custom failure')", "pytest.fail('custom failure')"]
)
def test_custom_item_traceback_filter(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generated with Claude assistance

These tests only check that def from_call( (runner.py) is gone. Could we add:

  • a check that pluggy frames (e.g. _multicall) are gone,
  • a case where the error is raised in a frame below runtest (ideally from a <generated> filename) and that frame is still shown,
  • a run with the default --tb=auto, since every case here uses --tb=long?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.

Added in 657e4aa: the main test now also checks _multicall and runs with both --tb=auto and --tb=long; new tests cover a <gen> frame below runtest (kept), a plugin pytest_runtest_call wrapper (cut), a failing conftest hook (leading cut), plus unit tests for cut_internal() and isgenerated().


Generated by Claude Code

@nicoddemus

nicoddemus commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Note

I used Claude (an AI assistant) to help with this review. I went through the findings myself, but please push back on anything that looks off.

Function._traceback_filter cuts the traceback at the test function, which drops the frames above it and keeps everything below it. This PR instead filters out every pytest, pluggy and <generated> frame across the whole traceback. That fixes the noise at the top, but it can also remove the frame where the error was actually raised. So custom items would now differ from test functions in the other direction (details inline).

I think a cut would be closer to what Function does: drop the leading frames up to the first one that isn't in pytest or pluggy, keep the rest, then apply filter(excinfo) for __tracebackhide__. That fixes #7692 without hiding frames below user code.

Some follow-ups on lines this PR doesn't touch:

  • src/_pytest/python.py:1910: when _obj isn't set, Function._traceback_filter still does return excinfo.traceback. Now that Node filters, a Function gets less filtering than a plain Item here. Should this be return super()._traceback_filter(excinfo)?
  • src/_pytest/nodes.py:634: same for path-less Collectors, which still return the raw traceback. super()._traceback_filter(excinfo) would be consistent.
  • src/_pytest/python.py:1891-1894: the innermost branch (filter(filter_traceback), then the fallback to the original traceback if that's empty) now duplicates Node._traceback_filter. Calling super() there would keep the two from drifting apart.

A possible shape for the cut, which would also tidy up the existing helpers:

  • Add a TracebackEntry.isinternal() method next to ishidden(), holding the pytest/pluggy path check that filter_traceback does today. filter_traceback can stay as a thin wrapper, so its other callers (config/__init__.py, python.py:557) keep working.
  • Keep generated code (<...> filenames) as a separate check, e.g. isgenerated(). Code compiled with such a filename is often user code (notebook and markdown runners), so calling it "internal" would be misleading.
  • Use it for a cut rather than a filter. For example, a Traceback.cut_internal() that drops leading entries while entry.isinternal() is true. Node._traceback_filter could then be cut_internal() followed by filter(excinfo), and Function's innermost fallback could reuse it.

I'd avoid putting this into Traceback.filter(excinfo) itself. That runs after the cut in Function and Collector, and is the default getrepr(tbfilter=True) path. So it would drop internal frames below user code everywhere, including the _pytest frames you need when debugging pytest's own test suite. It would also change behavior for plugins that call excinfo.traceback.filter(excinfo) on the public pytest.ExceptionInfo.

Per review: filtering dropped pytest/pluggy and generated frames below
the item too. Add TracebackEntry.isinternal()/isgenerated() and
Traceback.cut_internal(). Node._traceback_filter now starts at the
node's own method frame (like Function starts at the test function),
falling back to cutting the leading internal entries, then applies
__tracebackhide__. Function's innermost fallback and path-less
Collectors reuse it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014GZsRDDitRunjszt4LxLHz

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Traceback cleanup fails on Exception/pytest.fail in Item.runtest

3 participants