Discussion:
[issue25599] asyncio.iscoroutinefunction returns unexpected results when presented with unittest.mock.Mock
Theron Luhn
2015-11-10 17:26:22 UTC
Permalink
asyncio.iscoroutinefunction(unittest.mock.Mock())
<Mock name='mock._is_coroutine' id='4544068072'>

This is an unexpected response, both in type (Mock rather than boolean) and value (truthy).

inspect.iscoroutinefunction behaves as expected.
m = unittest.mock.Mock()
m._is_coroutine = False
asyncio.iscoroutinefunction(m)
False

----------
components: asyncio
messages: 254457
nosy: Theron Luhn, gvanrossum, haypo, yselivanov
priority: normal
severity: normal
status: open
title: asyncio.iscoroutinefunction returns unexpected results when presented with unittest.mock.Mock
type: behavior
versions: Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6

_______________________________________
Python tracker <***@bugs.python.org>
<http://bugs.python.org/issue25599>
_______________________________________
Guido van Rossum
2015-11-10 17:48:40 UTC
Permalink
Guido van Rossum added the comment:

Hm... I don't know what answer you would expect. Who says your mock shouldn't behave like a coroutine? In general, passing mocks to functions is likely to return a mock -- even boolean functions.

Can you provide more context?

----------

_______________________________________
Python tracker <***@bugs.python.org>
<http://bugs.python.org/issue25599>
_______________________________________
STINNER Victor
2015-11-10 21:53:45 UTC
Permalink
asyncio.iscoroutinefunction(unittest.mock.Mock())
<Mock name='mock._is_coroutine' id='4544068072'>

Yeah, I already had this issue when I wrote unit tests using mock :-/ I didn't find an obvious fix for this issue, and it's quite easy to workaround it.

It looks like the "m._is_coroutine = False" is not used in asyncio tests. I don't recall how I worked around the issue in asyncio tests...

----------

_______________________________________
Python tracker <***@bugs.python.org>
<http://bugs.python.org/issue25599>
_______________________________________

Loading...