Skip to content

Use direct cause for raises match failures#14391

Open
lavaFreak wants to merge 2 commits intopytest-dev:mainfrom
lavaFreak:fix-raises-match-cause
Open

Use direct cause for raises match failures#14391
lavaFreak wants to merge 2 commits intopytest-dev:mainfrom
lavaFreak:fix-raises-match-cause

Conversation

@lavaFreak
Copy link
Copy Markdown

Summary

  • raise the match-failure AssertionError with the original exception as its direct cause
  • add a regression test covering the direct-cause traceback wording
  • add the required changelog fragment and author entry

Why

When pytest.raises(..., match=...) fails because the regex does not match, the resulting traceback currently reports the original exception as implicit context. Raising the AssertionError with from exc_val preserves the original exception as the direct cause instead, which matches the issue expectation and produces clearer output.

Closes #14389.

Testing

  • env PYTHONPATH=/Users/garion/Work/pytest/src /Users/garion/Work/pytest/.venv/bin/python -m pytest /Users/garion/Work/pytest/testing/python/raises.py

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Apr 17, 2026
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.

Exception should be direct cause of AssertionError from raises context manager on failure for exception to match pattern rather than context

1 participant