Spy async bug introduced in Python 3.8.1
See original GitHub issueI believe bpo-38875 has introduced a bug in mocker.spy()
. Spied coroutines are no longer awaited as they should be.
This test highlights the issue:
import pytest
class MyClass:
async def method(self):
return "hi"
@pytest.mark.asyncio
async def test_spy_async(mocker):
my_obj = MyClass()
mocker.spy(my_obj, 'method')
returned = await my_obj.method()
# Passes
assert my_obj.method.called
# AssertionError: assert <coroutine object MyClass.method at 0x10f2a5d40> == 'hi'
assert returned == "hi"
It looks like spy()
(specifically, wrapper()
defined within spy()
) returns a coroutine and expects unittest
’s patch()
do to the awaiting, rather than doing the awaiting itself. This worked until bpo-38875 changed this behaviour. Now unittest
will no longer await retuned awaitables, rather it returns them.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Asyncio Module Not Working On Python 3.8 When Following ...
You cannot pass gather directly to asyncio.run . You need to wrap it instead a coroutine, eg async def main , that awaits...
Read more >asyncio — Asynchronous I/O — Python 3.11.1 documentation
Hello World! asyncio is a library to write concurrent code using the async/await syntax. asyncio is used as a foundation for multiple Python...
Read more >pytest-mock 3.6.1 - PyPI
The mocker.spy object acts exactly like the original method in all cases, except the spy also tracks function/method calls, return values and exceptions ......
Read more >Package List — Spack 0.20.0.dev0 documentation
Versions: master, 3.8.1, 3.7.3, 3.7.2, 3.7.0; Build Dependencies: cuda, cmake, ... interfaces that are implemented by other toolkits and applications.
Read more >Bug listing with status RESOLVED with resolution INVALID as ...
status:RESOLVED resolution:INVALID severity:enhancement · Bug:1084 - "python-ldap-2.0.0_pre04-r1.ebuild (New Package)" status:RESOLVED resolution:INVALID ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the ping @k4nar! Just released
3.0.0
with this fix. 👍Awesome, thank you for the responsiveness! Stay safe 😃 .