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

Refactor AsyncMock setup logic in create_autospec #81228

Closed
tirkarthi opened this issue May 25, 2019 · 2 comments
Closed

Refactor AsyncMock setup logic in create_autospec #81228

tirkarthi opened this issue May 25, 2019 · 2 comments
Labels
3.8 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@tirkarthi
Copy link
Member

BPO 37047
Nosy @cjw296, @voidspace, @asvetlov, @1st1, @lisroach, @mariocj89, @tirkarthi
PRs
  • bpo-37047: Refactor AsyncMock setup logic for autospeccing #13574
  • 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 = <Date 2019-05-27.12:56:57.361>
    created_at = <Date 2019-05-25.19:38:44.818>
    labels = ['3.8', 'type-bug', 'library']
    title = 'Refactor AsyncMock setup logic in create_autospec'
    updated_at = <Date 2019-05-27.12:56:57.360>
    user = 'https://github.com/tirkarthi'

    bugs.python.org fields:

    activity = <Date 2019-05-27.12:56:57.360>
    actor = 'yselivanov'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-05-27.12:56:57.361>
    closer = 'yselivanov'
    components = ['Library (Lib)']
    creation = <Date 2019-05-25.19:38:44.818>
    creator = 'xtreak'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37047
    keywords = ['patch']
    message_count = 2.0
    messages = ['343504', '343620']
    nosy_count = 7.0
    nosy_names = ['cjw296', 'michael.foord', 'asvetlov', 'yselivanov', 'lisroach', 'mariocj89', 'xtreak']
    pr_nums = ['13574']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37047'
    versions = ['Python 3.8']

    @tirkarthi
    Copy link
    Member Author

    • In create_autospec there is some logic to detect if the function is an async function this could be refactored out as a private function.

    • create_autospec has initialization code for async mock. For synchronous functions this is done with _setup_func and is called during setting signature. AsyncMock needs different setup logic code to setup the functions but the code can be refactored out of create_autospec into _setup_async_func.

    • In create_autospec awaited attribute is not set for AsyncMock during setup [0] and hence this causes AttributeError when the coroutine is awaited. awaited attribute can be also initialized.

    import asyncio
    from unittest.mock import create_autospec
    
    async def foo(): pass
    
    spec = create_autospec(foo)
    awaitable = spec()
    
    async def main(): await awaitable
    
    asyncio.run(main())
    Traceback (most recent call last):
      File "/tmp/spam.py", line 13, in <module>
        asyncio.run(main())
      File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/asyncio/runners.py", line 43, in run
        return loop.run_until_complete(main)
      File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/asyncio/base_events.py", line 614, in run_until_complete
        return future.result()
      File "/tmp/spam.py", line 11, in main
        await awaitable
      File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 2045, in _mock_call
        return await proxy()
      File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 2043, in proxy
        await self.awaited._notify()
      File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 596, in __getattr__
        raise AttributeError("Mock object has no attribute %r" % name)
    AttributeError: Mock object has no attribute 'awaited'
    • In the setup logic to create attributes like assert_not_awaited uses the pattern setattr(mock, a, f) at [1] . But due to late binding 'a' in the function f has the last value of the loop 'assert_not_awaited' and hence calling other helpers also calls assert_not_awaited. This could be resolved by using a partial function that binds the attribute value early in the loop and respective function would be used in getattr.
    >>> spec.assert_awaited_once_with(1) # Due to late binding assert_not_awaited is always called
    TypeError: assert_not_awaited() takes 1 positional argument but 2 were given
    • assert_not_awaited has the error message indicating it should be awaited once [2] . This can be changed to indicate something like "Expected mock to not have been awaited".
    >>> spec.assert_not_awaited()
    AssertionError: Expected mock to have been awaited once. Awaited 1 times.
    • mock docs have list of magic methods implemented where __aenter__, __aexit__, __aiter__ and __anext__ could be documented with versionadded directive. [3]

    I have a PR with the above changes that I will post shortly for review.

    [0]

    mock._is_coroutine = asyncio.coroutines._is_coroutine

    [1]
    def f(*args, **kwargs):

    [2]
    msg = (f"Expected {self._mock_name or 'mock'} to have been awaited once."

    [3] https://docs.python.org/3.8/library/unittest.mock.html#mocking-magic-methods

    @tirkarthi tirkarthi added 3.8 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 25, 2019
    @1st1
    Copy link
    Member

    1st1 commented May 27, 2019

    New changeset ff6b2e6 by Yury Selivanov (Xtreak) in branch 'master':
    bpo-37047: Refactor AsyncMock setup logic for autospeccing (GH-13574)
    ff6b2e6

    @1st1 1st1 closed this as completed May 27, 2019
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    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 stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants