This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author lisroach
Recipients hroncok, jcline, lisroach, mariocj89, xtreak
Date 2019-06-26.04:14:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Yes, sorry I wasn't clear, I was thinking about the functions and testing without your PR. I think removing the __code__ object (or working around it) is the correct way to go, but just removing it wouldn't solve this particular problem.

"If I understand the awaitable examples correctly, mocking the obj which is an Awaitable should be returning an AsyncMock. But obj doesn't contain __code__ and hence check for inspect.isawaitable is never done causing _is_async_obj(obj) to return False and subsequently it's patched with MagicMock."

Exactly! This is why I think technically removing the __code__ check is correct. Probably removing the __code__ attribute for any AsyncMock that is mocking an async object and not an async function is best, but I don't know how I would do that. 

I may also be misunderstanding some asyncio concepts, that is just what I observed :)

What if instead of checking for the __code__ object at all we check if there it is a Mock object (excluding AsyncMock):

    def _is_async_obj(obj):
        sync_mocks = [MagicMock, Mock, PropertyMock, NonCallableMock, NonCallableMagicMock]
        if (any(isinstance(obj, sync_mock) for sync_mock in sync_mocks)
                and not isinstance(obj, AsyncMock)):
            return False
        return asyncio.iscoroutinefunction(obj) or inspect.isawaitable(obj)
Date User Action Args
2019-06-26 04:14:59lisroachsetrecipients: + lisroach, hroncok, mariocj89, xtreak, jcline
2019-06-26 04:14:59lisroachsetmessageid: <>
2019-06-26 04:14:59lisroachlinkissue37251 messages
2019-06-26 04:14:58lisroachcreate