You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Convenient simplefilter() in warnings.catch_warnings() #91230
Many pytest users would like a concise way to either suppress warnings, or convert them to errors [1]. The current best approach is:
withwarnings.catch_warnings():
warnings.simplefilter("ignore") # or "error"
but even this becomes tediously verbose when repeated in hundreds of tests (which I suspect is responsible for many misuses of pytest.raises()). Since these two functions take disjoint arguments [2], I propose allowing catch_warnings() to accept arguments for simplefilter() and forward them on:
withwarnings.catch_warnings("ignore"): # or "error"
I have a quick implementation at [3] which I can finish if the idea is acceptable. Although I don't have a concrete use-case for more than the 'action' and 'category' arguments, forwarding all rather than some makes the semantics very simple.
I'd emphasize again that this is very much a convenience-focussed change: I estimate that 95% of all uses will pass only action="error" or action="ignore". Supporting a concise and simple interface for this seems worth the small complication to the design of the warnings module.
Activity
Zac-HD commentedon Mar 21, 2022
Many pytest users would like a concise way to either suppress warnings, or convert them to errors [1]. The current best approach is:
but even this becomes tediously verbose when repeated in hundreds of tests (which I suspect is responsible for many misuses of
pytest.raises()). Since these two functions take disjoint arguments [2], I propose allowing catch_warnings() to accept arguments for simplefilter() and forward them on:I have a quick implementation at [3] which I can finish if the idea is acceptable. Although I don't have a concrete use-case for more than the 'action' and 'category' arguments, forwarding all rather than some makes the semantics very simple.
I'd emphasize again that this is very much a convenience-focussed change: I estimate that 95% of all uses will pass only
action="error"oraction="ignore". Supporting a concise and simple interface for this seems worth the small complication to the design of the warnings module.[1] see e.g. pytest-dev/pytest#9404 for a long thread
[2] unlike
filterwarnings(), which has amodule=argument which would collide withcatch_warnings()[3] main...Zac-HD:zac-hd/one-line-catch-warnings
gh-91230: Concise catch_warnings with simplefilter (#91435)