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

All unittest.mock autospec-generated methods are coroutine functions #85794

Open
twm mannequin opened this issue Aug 24, 2020 · 3 comments
Open

All unittest.mock autospec-generated methods are coroutine functions #85794

twm mannequin opened this issue Aug 24, 2020 · 3 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

@twm
Copy link
Mannequin

twm mannequin commented Aug 24, 2020

BPO 41628
Nosy @lisroach, @tirkarthi, @twm
Files
  • repro4.py
  • 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-08-24.23:31:52.482>
    labels = ['3.8', 'type-bug', 'library', '3.9', '3.10']
    title = 'All unittest.mock autospec-generated methods are coroutine functions'
    updated_at = <Date 2020-08-25.09:17:33.582>
    user = 'https://github.com/twm'

    bugs.python.org fields:

    activity = <Date 2020-08-25.09:17:33.582>
    actor = 'xtreak'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2020-08-24.23:31:52.482>
    creator = 'twm'
    dependencies = []
    files = ['49425']
    hgrepos = []
    issue_num = 41628
    keywords = []
    message_count = 2.0
    messages = ['375862', '375864']
    nosy_count = 3.0
    nosy_names = ['lisroach', 'xtreak', 'twm']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41628'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    Linked PRs

    @twm
    Copy link
    Mannequin Author

    twm mannequin commented Aug 24, 2020

    Given a class:

        class Foo:
            def bar(self):
                pass

    And an autospec'd mock of it:

        foo_mock = mock.create_autospec(spec=Foo)

    The result of asyncio.iscoroutinefunction() differs:

        asyncio.iscoroutinefunction(Foo.bar)        # -> False
        asyncio.iscoroutinefunction(foo_mock.bar)   # -> True

    This behavior is the same on Python 3.7 and 3.8.

    I've attached a demonstration script, repro4.py. Here is the output on Python 3.7.9:

    $ python3.7 repro4.py 
    baz is a coroutine function?                        False
    Foo.bar is a coroutine function?                    False
    foo_instance.bar is a coroutine function?           False
    baz_mock is a coroutine function?                   False
    foo_mock.bar is a coroutine function?               True
    foo_mock_instance.bar is a coroutine function?      True
    foo_mock_mock.bar is a coroutine function?          False
    foo_mock_mock_instance.bar is a coroutine function? False
    foo_mock_instance.bar()      ->  <MagicMock name='mock().bar()' id='140602586963280'>
    foo_mock_mock_instance.bar() ->  <Mock name='mock().bar()' id='140602587010192'>

    And on Python 3.8.2:

    python3.8 repro4.py
    baz is a coroutine function? False
    Foo.bar is a coroutine function? False
    foo_instance.bar is a coroutine function? False
    baz_mock is a coroutine function? False
    foo_mock.bar is a coroutine function? True
    foo_mock_instance.bar is a coroutine function? True
    foo_mock_mock.bar is a coroutine function? True
    foo_mock_mock_instance.bar is a coroutine function? False
    foo_mock_instance.bar() -> <MagicMock name='mock().bar()' id='139847100001728'>
    foo_mock_mock_instance.bar() -> <Mock name='mock().bar()' id='139847100037968'>

    @twm twm mannequin added 3.7 (EOL) end of life 3.8 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 24, 2020
    @twm
    Copy link
    Mannequin Author

    twm mannequin commented Aug 24, 2020

    Note that this can interact poorly with AsyncMock, introduced in Python 3.8, because it causes a mock generated from a mock produces an object with async methods rather than regular ones. In Python 3.7 chaining mocks like this would produce regular methods. (This is how I discovered this issue.)

    @tirkarthi tirkarthi added 3.9 only security fixes 3.10 only security fixes and removed 3.7 (EOL) end of life labels Aug 25, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @Chadys
    Copy link

    Chadys commented Mar 21, 2025

    Got bitten by this issue also.
    In my case, it caused an issue because I was using sync_to_async and this is using iscoroutinefunction.
    From what I found out it is because of this final check done by iscoroutinefunction: bool(mock.__code__.co_flags & inspect.CO_COROUTINE), this always returns True with an autospecced MagicMock.
    With AsyncMock, self.__dict__['__code__'] is set to a specific value, the same should be done for MagicMock.
    It would be great for the opened PR to land as sync_to_async can be heavily used in a Django application :)

    My reproduction, on python 3.12

    # misc.py
    from asgiref.sync import sync_to_async
    
    
    class A:
        def method1(self):
            return 1
    
    
    async def use_method1():
        a = A()
        await sync_to_async(a.method1)()
    # test_misc.py
    import inspect
    from unittest.mock import patch, _is_async_func
    from misc import use_method1
    
    
    async def test_mock():
        await use_method1()
        with (
            patch("misc.A", autospec=True) as FakeA,
        ):
            assert inspect.isfunction(FakeA.method1)
            # FAIL AssertionError
            assert not _is_async_func(FakeA.method1)
            # FAIL AssertionError
            assert not inspect.iscoroutinefunction(FakeA.method1)
            # FAIL AssertionError
            assert not bool(FakeA.method1.__code__.co_flags & inspect.CO_COROUTINE)
            # FAIL raises TypeError: sync_to_async can only be applied to sync functions.
            await use_method1()

    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: No status
    Development

    No branches or pull requests

    2 participants