Repository navigation
Deprecate registering command line options with required=True - #15161
Draft
RonnyPfannschmidt wants to merge 2 commits into
Draft
RonnyPfannschmidt wants to merge 2 commits into
RonnyPfannschmidt wants to merge 2 commits into
Conversation
argparse rejects every invocation that misses a required option, so `pytest --fixtures`, `--markers`, `--collect-only` and friends fail as soon as a plugin or conftest registers one (#3042). pytest never planned for required options; they only worked because argparse supports them. Emit a new PytestRemovedIn11Warning when Parser.addoption() or OptionGroup.addoption() is called with required=True, pointing at the caller. Behavior is unchanged otherwise: required options keep blocking until removal. Document two replacements in the examples: - fail the tests that need the value from the fixture providing it (pytest.fail(..., pytrace=False)), and - abort the run with pytest.UsageError from a tryfirst pytest_runtestloop, the first hook that only runs for real test runs, explaining why pytest_configure, pytest_cmdline_main and pytest_sessionstart are too early. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PpHzgwNb2PZrvXbvHNRZ6Q
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PpHzgwNb2PZrvXbvHNRZ6Q
This branch has not been deployed
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.
Requested by Ronny · project thread
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
Before: a plugin or conftest could register an option with
required=True. argparse then rejects every invocation that doesn't pass it, includingpytest --fixtures,--markersand--collect-only(#3042). Nothing tells plugin authors that pytest never planned for required options.After:
Parser.addoption(..., required=True)andOptionGroup.addoption(..., required=True)emit a newPytestRemovedIn11Warningpointing at the caller's line. Required options keep blocking things as before until removal. The examples document two replacements:pytest.fail(..., pytrace=False). Only those tests error.--fixtures,--collect-only,-kselection and xdist all keep working.pytest.UsageErrorfrom atryfirstpytest_runtestloop, skipped under--collect-only. This exits with code 4 before any test runs, also under xdist.The docs also explain why the earlier hooks are the wrong place. These are the results of putting the same check in each hook:
pytest_configure--help,--markers,--fixtures,--co,--cache-showpytest_cmdline_main--help,--fixtures,--co,--cache-show(astrylastit never runs, becausemain's impl returns first)pytest_sessionstart/ collection hooks--fixtures,--co,--cache-showpytest_runtestloop(tryfirst,--coguarded)-kselections that don't need the valueRejected options:
UsageErrorfrom a fixture gives a full traceback per test and exit code 1, so it reads as a regular setup error.pytest_runtestloopcheck (only raise if a selected item uses the fixture) crashes xdist workers with INTERNALERROR, because the controller has no items.Supersedes #15158, which relaxed
requiredglobally during parsing and handed pluginsNonefor their required options.Refs #3042
🤖 Generated with Claude Code
https://claude.ai/code/session_01PpHzgwNb2PZrvXbvHNRZ6Q
Generated by Claude Code