-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
inspect.iscorutinefunction() returns False for unittest.mock.AsyncMock instances #84753
Comments
inspect.iscoroutinefunction() returns False for unittest.mock.AsyncMock instances while asyncio.iscoroutinefunction() returns True.
Confirmed with 3.8.2 and 3.9dev |
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 |
The two implementations of iscoroutinefunction are implemented differently: in inspect: and in asyncio: originally the asyncio version had only the _is_coroutine check: the 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. |
looks like this issue is because from unittest import mock
import inspect
m = mock.AsyncMock()
with mock.patch("inspect.isfunction", new=lambda x: True):
assert inspect.iscoroutinefunction(m) |
The inspect version was not working with unittest.mock.AsyncMock. This change moves the asyncio fix for AsyncMock in inspect module and make asyncio.iscoroutinefunction an alias of inspect.iscoroutinefunction.
pythonGH-94050) The inspect version was not working with unittest.mock.AsyncMock. The fix introduces special-casing of AsyncMock in `inspect.iscoroutinefunction` equivalent to the one performed in `asyncio.iscoroutinefunction`. Co-authored-by: Łukasz Langa <lukasz@langa.pl> (cherry picked from commit 4261b6b) Co-authored-by: Mehdi ABAAKOUK <sileht@sileht.net>
pythonGH-94050) The inspect version was not working with unittest.mock.AsyncMock. The fix introduces special-casing of AsyncMock in `inspect.iscoroutinefunction` equivalent to the one performed in `asyncio.iscoroutinefunction`. Co-authored-by: Łukasz Langa <lukasz@langa.pl> (cherry picked from commit 4261b6b) Co-authored-by: Mehdi ABAAKOUK <sileht@sileht.net>
…94050) (GH-94461) The inspect version was not working with unittest.mock.AsyncMock. The fix introduces special-casing of AsyncMock in `inspect.iscoroutinefunction` equivalent to the one performed in `asyncio.iscoroutinefunction`. Co-authored-by: Łukasz Langa <lukasz@langa.pl> (cherry picked from commit 4261b6b) Co-authored-by: Mehdi ABAAKOUK <sileht@sileht.net>
Fixed with #94050 and backported to 3.10 and 3.11. Thanks! ✨ 🍰 ✨ |
…94050) (GH-94460) The inspect version was not working with unittest.mock.AsyncMock. The fix introduces special-casing of AsyncMock in `inspect.iscoroutinefunction` equivalent to the one performed in `asyncio.iscoroutinefunction`. Co-authored-by: Łukasz Langa <lukasz@langa.pl> (cherry picked from commit 4261b6b) Co-authored-by: Mehdi ABAAKOUK <sileht@sileht.net>
The wording used in pythonGH-94050 suggests narrower scope than what was actually implemented. This commit clarifies the full impact of pythonGH-94050 in the change log.
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:
The text was updated successfully, but these errors were encountered: