Skip to content

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

Description

@moriyoshi
mannequin
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']

Activity

  1. moriyoshi commented on May 9, 2020

    moriyoshimannequin
    MannequinAuthor

    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

  2. added
    testsTests in the Lib/test dir
    type-bugAn unexpected behavior, bug, or error
    on May 9, 2020
  3. tirkarthi commented on May 9, 2020

    @tirkarthi
    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
  4. added
    stdlibStandard Library Python modules in the Lib/ directory
    and removed
    testsTests in the Lib/test dir
    on May 9, 2020
  5. iritkatriel commented on Aug 25, 2020

    @iritkatriel
    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.

  6. 17 remaining items

  7. added a commit that references this issue on Jun 30, 2022
  8. added a commit that references this issue on Jun 30, 2022
  9. ambv commented on Jun 30, 2022

    @ambv
    Contributor

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

  10. added 2 commits that reference this issue on Jun 30, 2022
  11. added a commit that references this issue on Jul 5, 2022
  12. added a commit that references this issue on Jul 5, 2022
  13. added a commit that references this issue on Jul 5, 2022
  14. added a commit that references this issue on Jul 5, 2022
  15. added a commit that references this issue on Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10 (EOL)end of life3.8 (EOL)end of life3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions