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

-W not working for warnings emitted from finalizer context #8021

Closed
belm0 opened this issue Nov 10, 2020 · 3 comments
Closed

-W not working for warnings emitted from finalizer context #8021

belm0 opened this issue Nov 10, 2020 · 3 comments

Comments

@belm0
Copy link

belm0 commented Nov 10, 2020

I'd like pytest to fail on "coroutine never awaited" RuntimeWarning. But something about these warnings cause pytest not to behave as expected: a matching -W spec actually causes the warning to be completely ignored.

I suspect it's because these warnings are issued on shutdown of the interpreter, after the pytest harness has exited.

def test_foo():
    async def bar():
        assert False

    bar()
$ pytest foo.py
====================================== test session starts ======================================
platform darwin -- Python 3.7.7, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
collected 1 item

foo.py .                                                                                  [100%]

======================================= warnings summary ========================================
foo.py::test_foo
foo.py:5: RuntimeWarning: coroutine 'test_foo.<locals>.bar' was never awaited
    bar()

-- Docs: https://docs.pytest.org/en/stable/warnings.html
================================= 1 passed, 1 warning in 0.02s ==================================
$ pytest -Werror foo.py
====================================== test session starts ======================================
platform darwin -- Python 3.7.7, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
collected 1 item

foo.py .                                                                                  [100%]

======================================= 1 passed in 0.04s =======================================
@oremanj
Copy link

oremanj commented Nov 10, 2020

"coroutine was never awaited", as well as most ResourceWarnings, occur in finalizer context (from a GC run or reference-count decrement), which can't raise exceptions. That means turning them into errors will just print some stderr, which pytest helpfully suppresses as long as all of your tests pass. If you run with -s I bet you'll see it.

I don't know if it's possible to tell if you're in finalizer context, but if so, perhaps pytest could improve this situation by installing a more intelligent warning filter that remembers that warnings occurred in this context and forces the test to fail.

@belm0
Copy link
Author

belm0 commented Nov 10, 2020

I see. Given the lack of options for detecting missing await at lint or test time, it would be great for pytest to improve the warning filter to handle finalizer context. Otherwise I'm grepping the pytest output...

@belm0 belm0 changed the title -W not working for certain warnings (at end of session) -W not working for warnings emitted from finalizer context Nov 11, 2020
@asottile
Copy link
Member

duplicate of #5676 -- though I think we didn't quite get far enough to figure out that it's the unraisable context there -- let's follow up there!

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

No branches or pull requests

3 participants