-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Context manager to ensure no warnings are issued #9745
Comments
My position continues to be:
I'd also argue against providing super-powerful combinations - libraries are best when they provide clean and composable pieces. If people have complex needs - they have access to all of Python! (including |
If the standard library # Status quo / implementation of proposed `with pytest.does_not_warn():`
with warnings.catch_warnings():
warnings.simplefilter("error")
# your code here... So I think it's worth proposing upstream that
|
Since I've gotten the above proposal into CPython 3.11, I'm closing this issue. For now people can use with warnings.catch_warnings(action="ignore"):
warnings.warn("This will be ignored")
with warnings.catch_warnings(action="error"):
warnings.warn("This will be an exception")
with pytest.warns():
pass
# This will be an error because a warning was expected |
That's awesome, thanks @Zac-HD! |
This is a direct continuation of #9404, where the API discussion would have taken too long to further hold up 7.0.0, and which was therefore "closed" by a documentation fix.
While I cannot reproduce the wealth of discussion from that other issue, there was the following summary by @The-Compiler:
In addition to explicitly enforcing the absence of warnings (and the other use-cases presented above), I also think it would be a very worthwhile to have a (higher-level?) API that eases switching between different scenarios. For example, I recently found myself writing the following:
In particular, the calculation is the same in both cases and it takes a lot of ceremony to capture the only relevant difference (the warning). In pseudo-code, the intent boils down to (ignoring the
match=
clause for now and reusing an idea from the last thread):Note this idea did not gain any traction at the time, but I don't have a better suggestion for now (as changing behaviour of
pytest.warn()
orpytest.except()
was considered infeasible due to huge spread; I also find it unattractive to switch the context-manager itself, e.g. to a putativepytest.does_not_warn()
).The text was updated successfully, but these errors were encountered: