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

Zac-HD
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 April 11, 2022 07:58
@JelleZijlstra JelleZijlstra self-requested a review April 18, 2022 16:16
Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 April 19, 2022 03:07
@Zac-HD
Copy link
Contributor Author

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed one more thing while re-reviewing.

Doc/library/warnings.rst Show resolved Hide resolved
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.

Convenient simplefilter() in warnings.catch_warnings()
4 participants