Lisa Roach <lisaroac...@gmail.com> added the comment:

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)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue37251>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to