Repository navigation
inspect.iscorutinefunction() returns False for unittest.mock.AsyncMock instances #84753
Description
Activity
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()) TrueConfirmed with 3.8.2 and 3.9dev
- addedtestsTests in the Lib/test dirTests in the Lib/test dir3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on May 9, 2020 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
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directoryand removedtestsTests in the Lib/test dirTests in the Lib/test dir
on May 9, 2020 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:
f951d28the inspect check was added later.
See also bpo-28703, where the asyncio version was fixed to handle mock objects. Looks like the inspect version needs to be fixed as well.
17 remaining items
- added a commit that references this issue
on Jun 30, 2022 Fixed with #94050 and backported to 3.10 and 3.11. Thanks! ✨ 🍰 ✨
- added a commit that references this issue
on Jul 5, 2022 - added a commit that references this issue
on Sep 2, 2026
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: