Skip to content
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

Closed
moriyoshi mannequin opened this issue May 9, 2020 · 5 comments
Closed

inspect.iscorutinefunction() returns False for unittest.mock.AsyncMock instances #84753

moriyoshi mannequin opened this issue May 9, 2020 · 5 comments
Labels
3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@moriyoshi
Copy link
Mannequin

moriyoshi mannequin commented May 9, 2020

BPO 40573
Nosy @moriyoshi, @1st1, @lisroach, @tirkarthi, @iritkatriel

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:

assignee = None
closed_at = None
created_at = <Date 2020-05-09.03:14:11.981>
labels = ['3.8', 'type-bug', 'library', '3.9', '3.10']
title = 'inspect.iscorutinefunction() returns False for unittest.mock.AsyncMock instances'
updated_at = <Date 2020-08-25.16:22:34.440>
user = 'https://github.com/moriyoshi'

bugs.python.org fields:

activity = <Date 2020-08-25.16:22:34.440>
actor = 'iritkatriel'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2020-05-09.03:14:11.981>
creator = 'moriyoshi'
dependencies = []
files = []
hgrepos = []
issue_num = 40573
keywords = []
message_count = 3.0
messages = ['368497', '368498', '375886']
nosy_count = 5.0
nosy_names = ['moriyoshi', 'yselivanov', 'lisroach', 'xtreak', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue40573'
versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

@moriyoshi
Copy link
Mannequin Author

moriyoshi mannequin commented May 9, 2020

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

@moriyoshi moriyoshi mannequin added tests Tests in the Lib/test dir 3.8 (EOL) end of life 3.9 only security fixes type-bug An unexpected behavior, bug, or error labels May 9, 2020
@tirkarthi
Copy link
Member

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

@tirkarthi tirkarthi added stdlib Python modules in the Lib dir and removed tests Tests in the Lib/test dir labels May 9, 2020
@iritkatriel
Copy link
Member

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:
f951d28

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.

@iritkatriel iritkatriel added 3.10 only security fixes labels Aug 25, 2020
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@graingert
Copy link
Contributor

graingert commented Jun 21, 2022

looks like this issue is because mock.AsyncMock() doesn't pass inspect.isfunction:

from unittest import mock
import inspect

m = mock.AsyncMock()
with mock.patch("inspect.isfunction", new=lambda x: True):
    assert inspect.iscoroutinefunction(m)

sileht added a commit to sileht/cpython that referenced this issue Jun 29, 2022
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.
ambv added a commit that referenced this issue Jun 30, 2022
)

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>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 30, 2022
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>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 30, 2022
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>
ambv pushed a commit that referenced this issue Jun 30, 2022
…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>
@ambv
Copy link
Contributor

ambv commented Jun 30, 2022

Fixed with #94050 and backported to 3.10 and 3.11. Thanks! ✨ 🍰 ✨

@ambv ambv closed this as completed Jun 30, 2022
@ambv ambv moved this to Done in Unittest issues Jun 30, 2022
ambv pushed a commit that referenced this issue Jun 30, 2022
…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>
ambv added a commit to ambv/cpython that referenced this issue Jul 5, 2022
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.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 5, 2022
…94554)

(cherry picked from commit a2a3f2c)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
ambv added a commit that referenced this issue Jul 5, 2022
…4559)

(cherry picked from commit a2a3f2c)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 5, 2022
…94554)

(cherry picked from commit a2a3f2c)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
ambv added a commit that referenced this issue Jul 5, 2022
…4560)

(cherry picked from commit a2a3f2c)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

4 participants