Skip to content

Deprecate registering command line options with required=True - #15161

Draft
RonnyPfannschmidt wants to merge 2 commits into
mainfrom
claude/project-thread-bvvq6z
Draft

RonnyPfannschmidt wants to merge 2 commits into
mainfrom
claude/project-thread-bvvq6z

Conversation

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

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, including pytest --fixtures, --markers and --collect-only (#3042). Nothing tells plugin authors that pytest never planned for required options.

After: Parser.addoption(..., required=True) and OptionGroup.addoption(..., required=True) emit a new PytestRemovedIn11Warning pointing at the caller's line. Required options keep blocking things as before until removal. The examples document two replacements:

  • Fail the tests that need the value: check in the fixture that provides it and call pytest.fail(..., pytrace=False). Only those tests error. --fixtures, --collect-only, -k selection and xdist all keep working.
  • Abort the whole run: raise pytest.UsageError from a tryfirst pytest_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:

hook breaks
pytest_configure --help, --markers, --fixtures, --co, --cache-show
pytest_cmdline_main --help, --fixtures, --co, --cache-show (as trylast it never runs, because main's impl returns first)
pytest_sessionstart / collection hooks --fixtures, --co, --cache-show
pytest_runtestloop (tryfirst, --co guarded) nothing informational; it does block -k selections that don't need the value

Rejected options:

  • Raising UsageError from a fixture gives a full traceback per test and exit code 1, so it reads as a regular setup error.
  • A selection-aware pytest_runtestloop check (only raise if a selected item uses the fixture) crashes xdist workers with INTERNALERROR, because the controller has no items.

Supersedes #15158, which relaxed required globally during parsing and handed plugins None for their required options.

Refs #3042

🤖 Generated with Claude Code

https://claude.ai/code/session_01PpHzgwNb2PZrvXbvHNRZ6Q


Generated by Claude Code

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
@RonnyPfannschmidt RonnyPfannschmidt self-assigned this Oct 11, 2026
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 11, 2026
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

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