Skip to content
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
merged 4 commits into from Apr 24, 2022

Conversation

Copy link
Contributor

@Zac-HD Zac-HD commented Apr 11, 2022

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(): plus warnings.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 that with warnings.catch_warnings(action="error"): (or action="ignore") would be a substantial win for many people, especially in tests.

I've specifically chosen simplefilter over filterwarnings because the latter has a conflicting module parameter.

@Zac-HD Zac-HD force-pushed the zac-hd/one-line-catch-warnings branch 6 times, most recently from 3bac2a9 to 2098a68 Compare Apr 11, 2022
@JelleZijlstra JelleZijlstra self-requested a review Apr 18, 2022
Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

The idea and implementation look solid.

Lib/warnings.py Outdated Show resolved Hide resolved
@JelleZijlstra JelleZijlstra self-assigned this Apr 19, 2022
@Zac-HD Zac-HD force-pushed the zac-hd/one-line-catch-warnings branch from 2098a68 to f7056ea Compare Apr 19, 2022
Doc/library/warnings.rst Outdated Show resolved Hide resolved
@Zac-HD
Copy link
Author

@Zac-HD Zac-HD commented Apr 19, 2022

Thanks @JelleZijlstra for your review! I've updated the docs accordingly; let me know if there's anything else 😄

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Noticed one more thing while re-reviewing.

Doc/library/warnings.rst Show resolved Hide resolved
@JelleZijlstra JelleZijlstra merged commit b4e0484 into python:main Apr 24, 2022
13 checks passed
@Zac-HD Zac-HD deleted the zac-hd/one-line-catch-warnings branch Apr 24, 2022
JelleZijlstra added a commit to python/typeshed that referenced this issue Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants