Fix nested caplog.filtering early removal - #14284
Merged
RonnyPfannschmidt merged 11 commits intoAug 26, 2026
Merged
Conversation
Closes pytest-dev#14189 by checking if the filter is already present before adding or removing it.
for more information, see https://pre-commit.ci
RonnyPfannschmidt
approved these changes
May 17, 2026
ncoghlan
reviewed
Aug 13, 2026
The makepyfile/makeini docstring reflow is not this repo's style: nothing in pyproject.toml asks for it and ruff format accepts the original layout. It doubled the diff and would conflict on backport. Co-Authored-By: Claude Opus 5 <ai@anthropic.com> Co-Authored-By: Claude Code <ai@anthropic.com>
cast(logging.Filter, some_function) lies to the type checker. Subclass logging.Filter like test_with_statement_filtering right above does. Co-Authored-By: Claude Opus 5 <ai@anthropic.com> Co-Authored-By: Claude Code <ai@anthropic.com>
Per @ncoghlan's review suggestion: skip the try/finally entirely when the filter is already present, so the add and its matching remove live in the same branch instead of behind two separate guards. No behaviour change. Co-Authored-By: Claude Opus 5 <ai@anthropic.com> Co-Authored-By: Claude Code <ai@anthropic.com>
Backport to 9.1.x: 💚 backport PR created✅ Backport PR branch: Backported as #14939 🤖 @patchback |
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.
Closes #14189. Nested
caplog.filteringusage was eagerly removing filters when the inner context manager exited, even if the filter was already present from the outer context manager. This PR ensures the filter is only removed if it wasn't already present when the context manager was entered.