Expose CaptureManager as public API
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
Research direction
Start by examining the private CaptureManager in _pytest.capture and the existing public exposure pattern for pytest.TerminalReporter. Add the public API entry for CaptureManager and verify that plugin authors can reference it through pytest.CaptureManager without importing _pytest.capture; update or add the relevant API coverage if the repository identifies it.
Written by the indexing model from the issue text.
Description
What's the problem this feature will solve?
Plugins that need to temporarily disable capture to write directly to the terminal reporter must reference CaptureManager for type annotations. The only way to obtain it today is through request.config.pluginmanager.getplugin("capturemanager"), which returns an untyped reference. To properly annotate this, plugin authors are forced to import from the private _pytest.capture module:
from _pytest.capture import CaptureManager
capture_manager = cast(CaptureManager | None, request.config.pluginmanager.getplugin("capturemanager"))
This creates a coupling to pytest internals that could break without notice across releases, and it's inconsistent with the direction pytest has taken by exposing TerminalReporter and FixtureRequest as public API.
Describe the solution you'd like
Expose CaptureManager as pytest.CaptureManager, following the same pattern used for pytest.TerminalReporter. This would allow plugin authors to write type-safe code without reaching into private modules.
I ran into this while modernizing pytest-print to stop importing from _pytest. Every other internal type the plugin used (SubRequest, TerminalReporter) had a public replacement, but CaptureManager does not.
Alternative Solutions
The current workaround is importing from _pytest.capture behind TYPE_CHECKING, which avoids runtime coupling but still ties the type annotations to private internals. Another option would be to use Any and lose type safety entirely, but that defeats the purpose of annotating in the first place.
Additional context
The relevant usage pattern is common across plugins that print to the terminal — they need to call capture_manager.global_and_fixture_disabled() as a context manager to temporarily suspend capture. This is the recommended approach in pytest's own documentation for writing terminal-aware plugins.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pytest-dev/pytest
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
pytest-dev/pytest#14514 · 6 comments ·
-
type: docs
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
pytest-dev/pytest#9825 · 7 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
pytest-dev/pytest#15067 ·
-
Difficulty 5/5 Over a week Newbie friendliness 32/100
pytest-dev/pytest#15064 · 3 comments ·
-
topic: fixtures type: bug
pytest-dev/pytest#15051 · 1 comment · 1 assignee ·
All issues in pytest-dev/pytest
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100