Skip to content

warnings: support for logging.captureWarnings #2449

Closed
@blueyed

Description

@blueyed

Moved from #2445 (comment):

The warnings plugin does not handle the case where the logging module's captureWarnings mechanism is used to route warnings through the logging module, and therefore allows to filter them:

logging.captureWarnings(True)
logging.getLogger('py.warnings').addFilter(filter_deprecation_warnings)

logging.captureWarnings monkeypatches warnings.showwarning (in Python 3.6) to log them instead (as a warning).
pytest could maybe check if logging._warnings_showwarning is not None to detect if warnings are supposed to go through logging.

With or without this in setup.cfg:

[tool:pytest]
filterwarnings =
    once::DeprecationWarning
    once::PendingDeprecationWarning

This mechanism is useful if you want to have fine-grained support on filtering, e.g. based on some (dynamic) path in the warning message.

I am not sure if it is worth the effort to do something in this regard, and if so what the best option would be: maybe transferring it into a log record and see if the py.warnings handlers would filter them?

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: warningsrelated to the warnings builtin plugintype: enhancementnew 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