Skip to content

Keep warning filters installed during collection (#13485) - #15129

Open
wspperrimh wants to merge 1 commit into
pytest-dev:mainfrom
wspperrimh:wspperrimh/collect-filterwarnings-persist
Open

wspperrimh wants to merge 1 commit into
pytest-dev:mainfrom
wspperrimh:wspperrimh/collect-filterwarnings-persist

Conversation

@wspperrimh

Copy link
Copy Markdown

Summary

Fixes #13485 — warning filters installed at import time (e.g. a module-level warnings.filterwarnings() call in a test module or conftest) were being reset before test execution.

catch_warnings_for_item wraps collection and config phases in warnings.catch_warnings, which restores the previous warnings.filters list on exit — discarding any filters that imported code installed. For the config and collect phases, the context now remembers which filters the contained block added and re-applies them after the catch context exits, so they take effect for the rest of the session. Filters installed during a runtest phase stay confined to their item, preserving per-test isolation.

This also unskips test_works_with_filterwarnings; it now runs via pytester.runpytest_subprocess(), since an in-process run cannot detect the bug (pytest's own filterwarnings = ["error"] ini setting leaks in and makes the warning raise regardless).

Test plan

  • testing/test_warnings.py — all pass, including the previously skipped test_works_with_filterwarnings plus two new regression tests (conftest-level filters persist; filters set inside a test do not leak). Verified the first two fail without the fix.

  • testing/test_recwarn.py, testing/test_collection.py — pass.

  • ruff check, ruff format and mypy clean on changed files.

  • Include documentation when adding new features.

  • Include new tests or update existing tests when applicable.

  • Allow maintainers to push and squash when merging my commits.

  • closes #13485 per the linking guidance.

  • AI agents were used and are credited in Co-authored-by commit trailers.

  • Added a changelog file (changelog/13485.bugfix.rst) and myself to AUTHORS.


AI disclosure: Authored by an AI agent fleet, human-supervised, per the AI/LLM-Assisted Contributions Policy — a human has reviewed, understands, and takes responsibility for this change; I'm able to explain and iterate on any part of it.

Warning filters installed by user code at import time (for example a
module-level `warnings.filterwarnings()` call in a test module or a
conftest) were lost when the warnings catch context wrapping collection
restored the previous filter list on exit. Re-apply filters installed
inside the collection and config phases so they take effect for the
rest of the session, while keeping filters installed by individual
tests confined to their item.

Co-authored-by: Devin AI <devin@cognition.ai>
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 4, 2026

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.

warnings: warning filters set by collected code during collection have no effect during test run

1 participant