Skip to content

Make pytest.exit()'s Exit a BaseException - #15442

Open
JelteF wants to merge 2 commits into
pytest-dev:mainfrom
JelteF:exit-baseexception
Open

JelteF wants to merge 2 commits into
pytest-dev:mainfrom
JelteF:exit-baseexception

Conversation

@JelteF

@JelteF JelteF commented Nov 23, 2026

Copy link
Copy Markdown

pytest.exit() raises Exit, which derives from Exception, unlike the other outcome exceptions (skip, fail, xfail) which derive from BaseException since #2490. So any except Exception: in a test or fixture swallows it, and the run continues. pytest itself already had to work around this in assertrepr_compare (#4978) and in pytest.warns (#11920).

This makes Exit derive from BaseException directly. It used to derive from KeyboardInterrupt and later from SystemExit (#4292), but pdb catches both of those itself, which is why it was changed to Exception (#4280).

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 23, 2026
The other outcome exceptions (`skip`, `fail`, `xfail`) derive from
`BaseException`, so that code catching `Exception` doesn't swallow them.
`Exit` derived from `Exception` though, so a test or fixture with an
`except Exception:` around code that calls `pytest.exit()` swallowed it, and
the run just continued. pytest itself had to work around this in
`pytest.warns` and in `assertrepr_compare`, which are now simplified.

`Exit` used to derive from `KeyboardInterrupt`, and later from `SystemExit`,
but was changed to `Exception` because pdb catches those two itself.
Deriving from `BaseException` directly avoids both problems.
@JelteF
JelteF force-pushed the exit-baseexception branch from 6290cc9 to e915ad2 Compare November 23, 2026 23:31
@JelteF
JelteF marked this pull request as ready for review November 23, 2026 23:31

@RonnyPfannschmidt RonnyPfannschmidt left a comment

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.

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

History: Exit went KeyboardInterrupt -> SystemExit in #4292 (pdb swallowed KeyboardInterrupt), then -> Exception in #4280 with no recorded reason. BaseException is the right base.

Two places still assume Exit is an Exception:

  1. SetupState.teardown_exact catches only TEST_OUTCOME. pytest.exit() in a fixture teardown now aborts the loop and the node's other finalizers never run. Please collect Exit there too and re-raise after the loop.
  2. pytest.exit() in an initial conftest or -p plugin used to become an import error; now it escapes config.main() as a raw traceback, rc 1, returncode ignored. main() should handle Exit.

Tests for both would be good. As except Exception / pytest.raises(Exception) no longer catch it, a breaking changelog entry fits better than improvement.


Generated by Claude Code

@JelteF
JelteF force-pushed the exit-baseexception branch 2 times, most recently from 9904e29 to db88f81 Compare November 26, 2026 11:38
Now that `Exit` is a `BaseException`, two places that used to catch it
as an `Exception` no longer did:

`SetupState.teardown_exact` only catches `TEST_OUTCOME`, so a
`pytest.exit()` in a fixture teardown stopped the loop and the other
finalizers never ran. It now runs them all and re-raises the `Exit`
afterwards. It is kept out of the collected teardown errors, because
wrapped in an exception group it would not stop the session anymore.

`pytest.exit()` in an initial conftest or a `-p` plugin escaped
`config.main()` as a raw traceback with exit code 1. It is now reported
like an `Exit` during the session, and its returncode is used. (Before
`Exit` became a `BaseException`, this was reported as an import error,
also ignoring the returncode.)
@JelteF
JelteF force-pushed the exit-baseexception branch from db88f81 to ac02cc0 Compare November 26, 2026 23:59

@RonnyPfannschmidt RonnyPfannschmidt left a comment

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.

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

Both fixes work here, and coverage is complete now.

One thing left for later, not this PR: when one finalizer calls pytest.exit() and another raises, the other error is dropped. That is the same problem as #13650 (Exit inside an exception group); once #13736 lands, teardown_exact can collect Exit into the group and keep both.


Generated by Claude Code

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.

2 participants