Skip to content

Improve handling of traceback elision - #4826

Open
Zac-HD wants to merge 3 commits into
HypothesisWorks:masterfrom
Zac-HD:claude/hypothesis-pr-review-iiu2yc
Open

Improve handling of traceback elision#4826
Zac-HD wants to merge 3 commits into
HypothesisWorks:masterfrom
Zac-HD:claude/hypothesis-pr-review-iiu2yc

Conversation

@Zac-HD

@Zac-HD Zac-HD commented Jul 26, 2026

Copy link
Copy Markdown
Member

Fixes #4681, closes #4696 (essentially an updated copy with merge conflicts fixed).

claude added 2 commits July 26, 2026 19:42
`@impersonate` used to overwrite `co_filename` and `co_firstlineno` on the
wrappers we generate, so that they claimed to be defined where the user's
test is.  That hid our internals from pytest and IPython, but at the cost
of wrong line numbers - a traceback would quote the `@given(...)` line
instead of the line which actually raised, and position exception carets
against the wrong source.

Instead we leave the code objects alone, and rely on the existing
`__hypothesistracebackhide__` marker plus `__tracebackhide__` in the
generated wrapper body.  Frames which do survive - under `--tb=native`, or
outside pytest - name `<hypothesis>` or a real file in our package.

`inspect.getsource` on a wrapped test used to work by accident, via the
faked location; `_patching` now follows `__wrapped_target` explicitly.

`test_healthcheck_traceback_is_hidden` asserted a magic distance between
two lines of pytest output, which no longer holds; a snapshot covering
this and several other scenarios replaces it in a later commit.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UioESJtvAnhpFvdyr4k9du
We already dropped our frames from the start of a traceback, but not from
between two frames of user code - so a test which draws from a strategy
that raises showed the whole engine in between.

The trimmed traceback is now built from fresh entries rather than by
reassigning `tb_next`, which would mutate a traceback that other code may
still hold a reference to.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UioESJtvAnhpFvdyr4k9du
@Zac-HD Zac-HD changed the title Claude/hypothesis pr review iiu2yc Improve handling of traceback elision Jul 26, 2026
Covers the scenarios from issue HypothesisWorks#4681 along with health checks, exception
groups, errors inside strategies, and an invalid @given signature.  Most
run under `--tb=native`, which unlike pytest's own rendering shows every
frame that survived trimming.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UioESJtvAnhpFvdyr4k9du
@Zac-HD
Zac-HD force-pushed the claude/hypothesis-pr-review-iiu2yc branch from 7e4334a to b44ba40 Compare July 26, 2026 20:17
@Liam-DeVoe

Copy link
Copy Markdown
Member

Let f be a function decorated with @given. Then:

  ┌────────────────────────────────────────┬─────────────────────────┬────────────────┐
  │             introspection              │         before          │     after      │
  ├────────────────────────────────────────┼─────────────────────────┼────────────────┤
  │ inspect.getsource(f)                   │ decorated test's source │ raises OSError │
  ├────────────────────────────────────────┼─────────────────────────┼────────────────┤
  │ inspect.getsourcefile(f) / co_filename │ test_loc.py             │ <hypothesis>   │
  ├────────────────────────────────────────┼─────────────────────────┼────────────────┤
  │ pytest item.location[0] (pytest 8.4)   │ test_loc.py             │ <hypothesis>   │
  └────────────────────────────────────────┴─────────────────────────┴────────────────┘

This is pretty unfortunate and seems likely to have bad downstream effects. A user would have to know about f.hypothesis.inner_test.

I'm pretty seriously considering alternatives in the shape of "rebuild the column mapping table ourselves"... I have claude going on a possible design in the background but I'm not sure it's going to pan out.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Impersonating functions can affect tracebacks in unintended ways

3 participants