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
pytest.raises swallows warning about unawaited coroutine #10404
Comments
This sounds like a classic "holding the traceback keeps everything in referenced frames alive" problem, which we will want to fix. That said, there is no guarantee that garbage collection will run at any particular time (or at all!) even if we do fix it, so I don't consider this a serious bug. |
Even if you do a |
A next generation raises could return a intentionally cleaned up traveback representation I'm not Fond of the py.code remnant, but it's quite a bit of work to make it nice |
I wasted some time reading all issues here to find how to fail a test when a coroutine was not awaited. In case if someone will need a solution: # pytest.ini
filterwarnings=
error:coroutine .* was never awaited:RuntimeWarning
# A trick to fail the `PytestUnraisableExceptionWarning: Exception ignored in:` which happens when we catch a `was never awaited` warning
error:.*Exception ignored in.*::_pytest |
That's unrelated to this issue though. We use |
Binding the return value of the
pytest.raises
context manager to a variable prevents some warnings from being triggered as part of the test. As a result, the warning is not displayed in the test run summary, but only on stderr, and-W error
will not fail the test.Reproducible example:
Running the above code:
When replacing
with pytest.raises(RuntimeError) as _:
withwith pytest.raises(RuntimeError):
, everything works as expected and the warning causes the test run to fail.My understanding is that the coroutine triggers the warning when it gets garbage collected. It looks like
with pytest.raises as _
keeps a reference to the coroutine that should trigger the warning. The reference prevents garbage collection of the coroutine until after the hook wrappers inunraisableexception
have returned. As a result, the warning appears outside of the test case.Additional information
Related issues: #8021 #5676
The text was updated successfully, but these errors were encountered: