Modernize ConftestImportFailure to use the exception chain - #14943
Draft
RonnyPfannschmidt wants to merge 2 commits into
Draft
Modernize ConftestImportFailure to use the exception chain#14943RonnyPfannschmidt wants to merge 2 commits into
RonnyPfannschmidt wants to merge 2 commits into
Conversation
RonnyPfannschmidt
force-pushed
the
conftest-import-failure-modernize
branch
3 times, most recently
from
August 27, 2026 07:21
efef5df to
c5e0b6b
Compare
A plugin failing to load during startup escaped `_main` as an unhandled exception: Python printed a raw traceback and exited 1, which is indistinguishable from EXIT_TESTSFAILED. Meanwhile a conftest.py failing to import already returned EXIT_USAGEERROR, which is the inconsistency pytest-dev#993 was filed about. Split the failure into the two things it can actually mean: - the plugin cannot be found at all -- pytest was pointed at something which is not there, so this is a usage error (exit 4), matching what conftest.py import failures already do. - the plugin was found but raised while importing -- including a missing transitive dependency and a broken pytest11 entry point -- which is a defect in the plugin rather than a misuse of pytest, so it is reported as an internal error (exit 3). The plugin traceback is preserved in both the report and the exception chain (PluginImportFailure is always raised `from` the original error); losing it was the main objection to the earlier attempt in pytest-dev#7290. Side effects: - pytest.main() now returns these exit codes instead of propagating the exception to its caller, matching its documented contract. - a bare `raise ImportError` in a plugin no longer crashes pytest's own internals with `IndexError: tuple index out of range` from `e.args[0]`. conftest.py import failures are deliberately left alone and keep returning exit 4. Co-Authored-By: Claude Opus 5 <ai@anthropic.com> Co-Authored-By: Claude Fable 5 <ai@anthropic.com> Co-Authored-By: Claude Code <ai@anthropic.com>
Give ConftestImportFailure the same shape PluginImportFailure just got: a bare Exception subclass whose argument is the conftest path, with the original error carried by `raise ... from` on __cause__ instead of a hand-rolled `cause` attribute duplicating it. The custom __init__ and __str__ go away; str(e) is the path, which is exactly what the two message consumers want. The `cause` attribute dates to 8.0 (e1074f9), which replaced the old excinfo triplet without deprecation; the class is private to _pytest.config and a code search finds no external consumers, only vendored copies of pytest itself. No user-visible output changes. Co-Authored-By: Claude Fable 5 <ai@anthropic.com> Co-Authored-By: Claude Code <ai@anthropic.com>
RonnyPfannschmidt
force-pushed
the
conftest-import-failure-modernize
branch
from
August 27, 2026 19:58
c5e0b6b to
3e02d6b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #14824 — review only the last commit; the first is that PR.
Gives
ConftestImportFailurethe same shapePluginImportFailuregot in #14824: a bareExceptionsubclass whose argument is the conftest path, with the original error carried byraise ... fromon__cause__instead of a hand-rolledcauseattribute duplicating the chain. The custom__init__and__str__go away;str(e)is the path, which is exactly what both message consumers want.Why this is safe:
causeattribute is young — introduced in 8.0 (e1074f9) replacing the oldexcinfotriplet, without deprecation;_pytest.config(not exported aspytest.*), and a GitHub code search finds no external importers, only vendored copies of pytest itself;print_conftest_import_error, the--help/--versiongrace path,Node._repr_failure_pyunwrapping,--pdbpost-mortem unwrapping,_main) are updated in this PR.No user-visible output changes.
🤖 Written by Claude Code (Claude Fable 5); reviewed and submitted by @RonnyPfannschmidt.