Open kanetkarster opened 7 months ago
happy to submit a PR if folks think this is the right course of action
This is intended as mock.__call__
is inherited from CallableMixin
whose __call__
method is not a couroutine function but when it is called by the instance of AsyncMock
, it returns a coroutine.
>>> from unittest import mock
>>> issubclass(mock.AsyncMock, mock.CallableMixin)
True
>>> mock.AsyncMock().__call__
<bound method CallableMixin.__call__ of <AsyncMock id='...'>>
>>> import inspect
>>> inspect.iscoroutinefunction(mock.AsyncMock().__call__)
False
>>> inspect.iscoroutine(mock.AsyncMock().__call__())
True
>>> inspect.isawaitable(mock.AsyncMock().__call__())
True
This is not a problem because the instance of AsyncMock
will be always awaitable even if the spec
class has regular __call__
method which in that case you should use MagicMock
instead.
This issue can be closed.
@infohash - not sure I agree that this can be closed. The issue reported here is that inspect
gives a different answer for a specced mock than it does for the original object. I could see this being important for some use cases.
Perhaps AsyncMock
should have its own __call__
method that is sync def
?
Perhaps
AsyncMock
should have its own__call__
method that isasync def
?
@cjw296 This will still give a different answer if the original class implements __call__
as a regular method.
import inspect
from unittest import mock
class AsyncCallableClass:
def __call__(self):
pass
>>> async_mocked_class = mock.AsyncMock(spec=AsyncCallableClass)
>>> inspect.iscoroutinefunction(AsyncCallableClass.__call__)
False
>>> inspect.iscoroutinefunction(async_mocked_class.__call__)
True
What matters to the user code is that, it should be able to await the value returned by __call__
method of AsyncMock
which is already the case,
Bug description:
repro:
output:
I expected the output for the second line to be
True
but it appearsAsync|MagicMock(spec=MySpec)
doesn't check calli believe the issues is that _is_async_obj should be:
CPython versions tested on:
3.10
Operating systems tested on:
No response