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.

classification
Title: inspect.iscorutinefunction() returns False for unittest.mock.AsyncMock instances
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, lisroach, moriyoshi, xtreak, yselivanov
Priority: normal Keywords:

Created on 2020-05-09 03:14 by moriyoshi, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg368497 - (view) Author: Moriyoshi Koizumi (moriyoshi) Date: 2020-05-09 03:14
inspect.iscoroutinefunction() returns False for unittest.mock.AsyncMock instances while asyncio.iscoroutinefunction() returns True.

```
>>> import unittest.mock
>>> import inspect
>>> import asyncio
>>> inspect.iscoroutinefunction(unittest.mock.AsyncMock())
False
>>> asyncio.iscoroutinefunction(unittest.mock.AsyncMock())
True
```

Confirmed with 3.8.2 and 3.9dev
msg368498 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-05-09 04:03
I couldn't reproduce the change in result for consecutive calls on master branch. They should return the same value.

./python   
Python 3.9.0a6+ (heads/master:7f7e706d78, May  9 2020, 04:00:36) 
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import unittest.mock
>>> import inspect
>>> import asyncio
>>> inspect.iscoroutinefunction(unittest.mock.AsyncMock())
False
>>> inspect.iscoroutinefunction(unittest.mock.AsyncMock())
False
>>> inspect.iscoroutinefunction(unittest.mock.AsyncMock())
False
msg375886 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-08-25 10:58
The two implementations of iscoroutinefunction are implemented differently: 

in inspect:
def iscoroutinefunction(obj):
    """Return true if the object is a coroutine function.
    Coroutine functions are defined with "async def" syntax.
    """
    return _has_code_flag(obj, CO_COROUTINE)

and in asyncio: 
def iscoroutinefunction(func):
    """Return True if func is a decorated coroutine function."""
    return (inspect.iscoroutinefunction(func) or
            getattr(func, '_is_coroutine', None) is _is_coroutine)


originally the asyncio version had only the _is_coroutine check:
https://github.com/python/cpython/commit/f951d28ac890063e3ecef56aa8cf851b1152d9dd

the inspect check was added later.

See also issue28703, where the asyncio version was fixed to handle mock objects.  Looks like the inspect version needs to be fixed as well.
History
Date User Action Args
2022-04-11 14:59:30adminsetgithub: 84753
2020-08-25 16:22:34iritkatrielsetnosy: + yselivanov
2020-08-25 10:59:23iritkatrielsetversions: + Python 3.10
2020-08-25 10:58:19iritkatrielsetnosy: + iritkatriel
messages: + msg375886
2020-05-09 04:03:39xtreaksetmessages: + msg368498
components: + Library (Lib), - Tests
2020-05-09 03:52:54xtreaksetnosy: + lisroach, xtreak
2020-05-09 03:14:12moriyoshicreate