Repository navigation
Keep warning filters installed during collection (#13485) - #15129
Open
wspperrimh wants to merge 1 commit into
Open
wspperrimh wants to merge 1 commit into
wspperrimh wants to merge 1 commit into
Conversation
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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_itemwraps collection and config phases inwarnings.catch_warnings, which restores the previouswarnings.filterslist on exit — discarding any filters that imported code installed. For theconfigandcollectphases, 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 aruntestphase stay confined to their item, preserving per-test isolation.This also unskips
test_works_with_filterwarnings; it now runs viapytester.runpytest_subprocess(), since an in-process run cannot detect the bug (pytest's ownfilterwarnings = ["error"]ini setting leaks in and makes the warning raise regardless).Test plan
testing/test_warnings.py— all pass, including the previously skippedtest_works_with_filterwarningsplus 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 formatandmypyclean 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 #13485per the linking guidance.AI agents were used and are credited in
Co-authored-bycommit trailers.Added a changelog file (
changelog/13485.bugfix.rst) and myself toAUTHORS.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.