New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-91230: Concise catch_warnings with simplefilter #91435
Merged
JelleZijlstra
merged 4 commits into
python:main
from
Zac-HD:zac-hd/one-line-catch-warnings
Apr 24, 2022
Merged
gh-91230: Concise catch_warnings with simplefilter #91435
JelleZijlstra
merged 4 commits into
python:main
from
Zac-HD:zac-hd/one-line-catch-warnings
Apr 24, 2022
Conversation
This file contains 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
AlexWaygood
reviewed
Apr 11, 2022
Misc/NEWS.d/next/Library/2022-04-10-17-12-23.gh-issue-91230.T1d_fG.rst
Outdated
Show resolved
Hide resolved
3bac2a9
to
2098a68
Compare
2098a68
to
f7056ea
Compare
JelleZijlstra
reviewed
Apr 19, 2022
Thanks @JelleZijlstra for your review! I've updated the docs accordingly; let me know if there's anything else |
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.
Closes #91230, which was in turn motivated by pytest-dev/pytest#9404 (comment) and pytest-dev/pytest#9745 (comment): users evidently want a concise context manager to ignore all warnings in some block of code, and to error on warnings on some block of code.
Of course that can be done with the standard library
with warnings.catch_warnings():
pluswarnings.simplefilter(...)
; but in practice this is quite rare. I believe that the trivial friction of needing two statements plays a substantial role here, and therefore thatwith warnings.catch_warnings(action="error"):
(oraction="ignore"
) would be a substantial win for many people, especially in tests.I've specifically chosen
simplefilter
overfilterwarnings
because the latter has a conflictingmodule
parameter.