question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spy async bug introduced in Python 3.8.1

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
nicoddemuscommented, Mar 31, 2020

Thanks for the ping @k4nar! Just released 3.0.0 with this fix. 👍

1reaction
k4narcommented, Mar 31, 2020

Awesome, thank you for the responsiveness! Stay safe 😃 .

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found