Skip to content

Expose CaptureManager as public API #14186

@gaborbernat

Description

@gaborbernat

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: enhancementnew feature or API change, should be merged into features branchtype: feature-branchnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions