Classify plugin startup import failures (#993) - #14824
Draft
RonnyPfannschmidt wants to merge 1 commit into
Draft
Conversation
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 PluginImportFailure cause; 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 (1M context) <noreply@anthropic.com>
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.
Closes #993.
The state of #993
The issue reproduces unchanged on
main. What it reports asEXIT_TESTSFAILEDis not actually a chosen exit code:PytestPluginManager.import_pluginre-raises a plainImportError, which is neitherUsageErrornorConftestImportFailure, so it escapes_mainentirely. CPython prints a raw traceback and exits 1. The collision withEXIT_TESTSFAILEDis a coincidence.Two consequences the issue does not mention:
pytest.main()raises instead of returning, contradicting its documented "returns an exit code" contract.raise ImportError, no args) additionally crashes pytest's own internals withIndexError: tuple index out of rangefrome.args[0], so the user never even sees their plugin's failure.The rule this PR implements
The previous attempt (#7290) stalled on @bluetech's question: is a plugin failing under a top-level conftest's
pytest_pluginsa conftest problem or a plugin problem? This PR answers it by splitting on what actually went wrong rather than on which mechanism requested the plugin:ExitCode.USAGE_ERROR(4), the same codeconftest.pyimport failures already return.pytest11entry point. That is a defect in the plugin, not misuse →ExitCode.INTERNAL_ERROR(3).Discrimination is on
ModuleNotFoundError.namematching the requested import spec (or a parent package of it), so a plugin whose ownimport foofails is not misattributed to the user.The other objection to #7290 was that routing through
ConftestImportFailurehid the stack trace. Here the traceback is preserved both in the printed report and onPluginImportFailure.cause.conftest.pyimport failures are deliberately unchanged and keep returning 4.Resulting behavior
-p nosuchplugin/pytest_plugins/PYTEST_PLUGINSnaming a missing moduleERROR: Error importing plugin "…"pytest11entry pointconftest.pyfails to importpytest.main()in-processImportErrorOpen questions for discussion
This is a draft because the classification is a judgement call and the exit code is observable API.
INTERNAL_ERROR(3) the right bucket for a broken plugin? The original issue proposed 3 for everything.wrap_sessionalready uses 3 for exceptions during the session, including exceptions raised by plugins, so this is at least self-consistent — but 3 is documented as "internal error" and a third-party plugin blowing up is arguably not pytest's internals. The alternative is a new exit code, which is a bigger commitment.conftest.pyfollow the same rule? Today any exception from a conftest is 4, so a conftest with a genuine bug in it reports as user misuse. Leaving it alone keeps the blast radius small, but it means the two paths are still not fully symmetric.PytestPluginManager.load_setuptools_entrypointsis overridden so a broken installed plugin also gets 3. Without it a broken installed plugin still exits 1 with a raw traceback, which felt like an incomplete fix — but it does widen what this PR touches.bugfix. It does change observable exit codes, so it may warrantbreakinginstead — CI keying on 1 vs 3/4 would notice.Tests
New
TestStartupPluginImportErrorsintesting/acceptance_test.pycovers every entry path (-p,pytest_plugins,PYTEST_PLUGINS, entry point), the bare-ImportErrorregression, missing-plugin vs missing-dependency, missing submodule of an existing package, and a guard that conftest stays 4.Six existing tests were updated from
ImportErrortoUsageError/PluginImportFailure.test_importplugin_error_message(#375, #1998) still asserts the plugin's own frame is reachable, now through the cause.Full suite and pre-commit (incl. mypy) pass locally.
🤖 Generated with Claude Code