8000 bpo-26467: Adds AsyncMock for asyncio Mock library support by lisroach · Pull Request #9296 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-26467: Adds AsyncMock for asyncio Mock library support #9296

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

Merged
merged 27 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4353041
Initial commmit adding asyncio mock support.
lisroach May 14, 2018
b83e5a5
Adding async support to the mock library.
lisroach Jul 22, 2018
a9ea983
Removes superfluous changes.
lisroach Aug 16, 2018
50581e3
Cleans up comments.
lisroach Aug 16, 2018
96ddb0e
Fixes inspect and attribute error issues.
lisroach Sep 10, 2018
a4d4dbc
Fixes test_unittest changing env because of version issue.
lisroach Sep 11, 2018
bfdd5a7
Removes newlines from inspect.
lisroach Sep 12, 2018
ed7f13c
Removes unneeded comment and newlines.
lisroach Sep 12, 2018
34fa74e
Fixes async tests. Removes inspect fix.
lisroach Sep 14, 2018
302ef64
Fixes environment test issue.
lisroach Sep 14, 2018
bf749ac
Adds argument tests.
lisroach Sep 14, 2018
30b64b5
Adding the side_effect exception test.
lisroach Sep 14, 2018
5edac2a
Changes CoroutineMock to AsyncMock. Removes old-style coroutine refer…
lisroach May 7, 2019
fa978cc
Merge branch 'master' into asyncio_mock
lisroach May 7, 2019
24920a6
Changes fnmatch to list comp.
lisroach May 7, 2019
aec3153
Fixes import and a rebase.
lisroach May 7, 2019
45dddb7
Merge branch 'master' of https://github.com/python/cpython into async…
lisroach May 7, 2019
c0a88a9
Updates news with AsyncMock name change.
lisroach May 7, 2019
f9bee6e
Removes extraneous comments.
lisroach May 7, 2019
81ad0d1
Fixes RunTime warnings and missing io import.
lisroach May 8, 2019
c260104
Changes check to use issubclass instead of !=.
lisroach May 8, 2019
ae13db1
Adds AsyncMock docs and tests for iterators and context managers.
lisroach May 13, 2019
68dff1b
Uncomments commented out test.
lisroach May 13, 2019
64301e2
Fixes based on comments.
lisroach May 17, 2019
c7cd95e
Fixes broken docs.
lisroach May 17, 2019
033f7d3
Fixes broken doc await_arg.
lisroach May 18, 2019
2fef02c
Adds shoutout to Martin Richard for asynctest.
lisroach May 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixes inspect and attribute error issues.
  • Loading branch information
lisroach committed Sep 12, 2018
commit 96ddb0e32945f835f01a16f6969d533ea9952b77
7 changes: 2 additions & 5 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,8 @@ def iscoroutinefunction(object):

Coroutine functions are defined with "async def" syntax.
"""
try:
return bool((isfunction(object) or ismethod(object)) and
object.__code__.co_flags & CO_COROUTINE)
except AttributeError:
pass
return bool((isfunction(object) or ismethod(object)) and
object.__code__.co_flags & CO_COROUTINE)


def isasyncgenfunction(object):
Expand Down
20 changes: 15 additions & 5 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@


def _is_coroutine_obj(obj):
return asyncio.iscoroutinefunction(obj) or asyncio.iscoroutine(obj)
if getattr(obj, '__code__', None):
return asyncio.iscoroutinefunction(obj) or asyncio.iscoroutine(obj)
else:
return False


def _is_instance_mock(obj):
Expand Down Expand Up @@ -461,7 +464,7 @@ def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
_spec_coroutines = []

for attr in dir(spec):
if asyncio.iscoroutinefunction(getattr(spec, attr)):
if asyncio.iscoroutinefunction(getattr(spec, attr, None)):
Copy link
@f0k f0k Feb 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Late note, not sure if anyone gets a notification for this: This broke a use case for me. Before this change, the Mock constructor did not call getattr on any of the attributes of the mocked object specification. I used it to mock an instance of a base class that does not have all its properties implemented, using the **kwargs in the Mock constructor to ensure that unimplemented properties that were accessed by any tests got a sane value. I can solve this by using a subclass that does not have unimplemented properties. But maybe it would be wise to skip all attributes here that appear in **kwargs, because they will be subsequently overwritten?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if anyone gets a notification for this

For this reason it's worth creating a new issue and mentioning this PR there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@f0k - better yet, if you could work up a PR with a test that shows the problem you encountered, along with a fix for it, that would be better!

Copy link
Member
@tirkarthi tirkarthi Feb 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@f0k some related issues . Please use search before creating the issue so that you can check for previous issues and make sure it's not duplicate. Thanks.

#89917
#85934

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this reason it's worth creating a new issue

I wasn't sure yet whether this warrants a fix, and was aiming low :)

some related issues

Good spot, yes, they're both the same problem, and there are already three PRs proposing a fix. I will continue the discussion in #85934, as it's the older one of the two issues.

_spec_coroutines.append(attr)

if spec is not None and not _is_list(spec):
Expand Down Expand Up @@ -1054,7 +1057,10 @@ def __init__(self, spec=None, side_effect=None, return_value=DEFAULT,
wraps=None, name=None, spec_set=None, parent=None,
_spec_state=None, _new_name='', _new_parent=None, **kwargs):
self.__dict__['_mock_return_value'] = return_value

# Makes inspect.iscoroutinefunction() return False when testing a Mock.
code_mock = NonCallableMock(spec_set=CodeType)
code_mock.co_flags = 0
self.__dict__['__code__'] = code_mock
_safe_super(CallableMixin, self).__init__(
spec, wraps, name, spec_set, parent,
_spec_state, _new_name, _new_parent, **kwargs
Expand Down Expand Up @@ -2107,9 +2113,10 @@ def __init__(self, *args, **kwargs):
self.__dict__['_mock_await_args'] = None
self.__dict__['_mock_await_args_list'] = _CallList()
code_mock = NonCallableMock(spec_set=CodeType)
code_mock.co_flags = 0
code_mock.co_flags = 129
self.__dict__['__code__'] = code_mock


def _mock_call(_mock_self, *args, **kwargs):
try:
result = super()._mock_call(*args, **kwargs)
Expand Down Expand Up @@ -2530,7 +2537,10 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
spec = type(spec)

is_type = isinstance(spec, type)
is_coroutine_func = asyncio.iscoroutinefunction(spec)
if getattr(spec, '__code__', None):
is_coroutine_func = asyncio.iscoroutinefunction(spec)
else:
is_coroutine_func = False
_kwargs = {'spec': spec}
if spec_set:
_kwargs = {'spec_set': spec}
Expand Down
10 changes: 9 additions & 1 deletion Lib/unittest/test/testmock/testasync.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import inspect
import unittest

from unittest.mock import call, CoroutineMock, patch, MagicMock
Expand Down Expand Up @@ -115,10 +116,16 @@ def test_async_decorator():


class CoroutineMockTest(unittest.TestCase):
def test_iscoroutinefunction(self):
def test_iscoroutinefunction_default(self):
mock = CoroutineMock()
self.assertTrue(asyncio.iscoroutinefunction(mock))

def test_iscoroutinefunction_function(self):
async def foo(): pass
mock = CoroutineMock(foo)
self.assertTrue(asyncio.iscoroutinefunction(mock))
self.assertTrue(inspect.iscoroutinefunction(mock))

def test_iscoroutine(self):
mock = CoroutineMock()
self.assertTrue(asyncio.iscoroutine(mock()))
Expand Down Expand Up @@ -178,6 +185,7 @@ def test_target_coroutine_spec_not(self):
@patch.object(AsyncFoo, 'coroutine_method', spec=NormalFoo.a)
def test_async_attribute(mock_method):
self.assertTrue(isinstance(mock_method, MagicMock))
self.assertFalse(inspect.iscoroutinefunction(mock_method))

test_async_attribute()

Expand Down
7 changes: 7 additions & 0 deletions Lib/unittest/test/testmock/testcallable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# E-mail: fuzzyman AT voidspace DOT org DOT uk
# http://www.voidspace.org.uk/python/mock/

import inspect
import unittest
from unittest.test.testmock.support import is_instance, X, SomeClass

Expand Down Expand Up @@ -146,6 +147,12 @@ def test_create_autospec_instance(self):

self.assertRaises(TypeError, mock.wibble, 'some', 'args')

def test_inspect_iscoroutine_function(self):
def foo(): pass

mock = Mock(foo)
self.assertFalse(inspect.iscoroutine(mock))


if __name__ == "__main__":
unittest.main()
1 change: 0 additions & 1 deletion Lib/unittest/test/testmock/testhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,6 @@ class RaiserClass(object):
@staticmethod
def existing(a, b):
return a + b

s = create_autospec(RaiserClass)
self.assertRaises(TypeError, lambda x: s.existing(1, 2, 3))
s.existing(1, 2)
Expand Down
0