Preserve warning filters set during collection for the test run - #14879
Open
arronKler wants to merge 3 commits into
Open
Preserve warning filters set during collection for the test run#14879arronKler wants to merge 3 commits into
arronKler wants to merge 3 commits into
Conversation
Collection is wrapped in warnings.catch_warnings, so module-level warnings.filterwarnings() calls were discarded when collection finished and had no effect while tests ran. Keep those newly added filters and re-apply them in later catching contexts. Fixes pytest-dev#13485
typeshed types warnings.filters as Sequence, but it is a mutable list and we need to prepend filters persisted from collection.
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
pytest_collectionis wrapped inwarnings.catch_warnings, so anywarnings.filterwarnings()/simplefilter()calls made by collected modules orconftest.pywere discarded when that context exited. The later per-test catching context then started from the pre-collection filters, which is why module-level filters had no effect during the run (regression of #2430, introduced when collection-time warning capture was added).The existing
test_works_with_filterwarningsdid not catch this unless the suite was run with-Wdefault, because the in-process inner run inherited the outer suite'sfilterwarnings=error.Solution
Remember filters that user code adds while the initial-conftest and collection catching contexts are active. Re-apply them (in front of pytest's configured filters) in later catching contexts, and promote them into the enclosing warnings context when those phases finish.
@pytest.mark.filterwarningsis still applied after that, so marks keep precedence.test_works_with_filterwarningsnow runs in a subprocess with-Wdefault. Additional tests cover ignore filters from a collected module and fromconftest.py.Fixes #13485
closes #XYZW/Fixes #13485.changelog/13485.bugfix.rstAUTHORS