This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: AsyncMock force always creating an AsyncMock for child mocks
Type: behavior Stage: patch review
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: lisroach Nosy List: cjw296, ezio.melotti, lisroach, matrixise, michael.foord, pconnell, xtreak
Priority: low Keywords: patch

Created on 2019-09-11 17:24 by lisroach, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 16099 merged python-dev, 2019-09-13 13:15
PR 16137 closed matrixise, 2019-09-14 07:22
Messages (4)
msg351991 - (view) Author: Lisa Roach (lisroach) * (Python committer) Date: 2019-09-11 17:24
This idea has been brought up in person before and also discussed on AsyncTest: https://github.com/Martiusweb/asynctest/issues/100


It could be useful if someone has a lot of attributes that are also async that need to be mocked. 

It could probably be done with a flag that gets passed on to _get_child_mock and overrides the if statement to always return an AsyncMock.


Looking mostly for if people think this is a good idea/bad idea and help see around any corners that may be there.
msg352000 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-09-11 17:41
Sorry, is there an example of this use case. I went through the issue but have trouble understanding it since there are different suggestions. I guess child mock is always an AsyncMock irrespective of sync/async as I understand. We already had some reports over detecting async while patching to return AsyncMock and also had to document it to the users along with covering different ways of async functions.


from unittest.mock import AsyncMock


class Foo:

    def foo(self):
        pass

    async def bar(self):
        pass

m = AsyncMock(Foo)
f = m()
print(m.foo)
print(m.bar)


$ python3.8 /tmp/bar.py
<AsyncMock name='mock.foo' id='4313089744'>
<AsyncMock name='mock.bar' id='4313395696'>
sys:1: RuntimeWarning: coroutine 'AsyncMockMixin._mock_call' was never awaited
msg352101 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2019-09-12 10:51
That may change though, right?

In general I dislike the proliferation of keyword arguments if it's at all possible to avoid. I added way too many of them originally and the mock API is really big.
msg352360 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2019-09-13 16:40
New changeset 14fd925a18fe3db0922a7d798e373102fe7a8a9c by Stéphane Wirtel (Michael Foord) in branch 'master':
bpo-38122: minor fixes to AsyncMock spec handling (GH-16099)
https://github.com/python/cpython/commit/14fd925a18fe3db0922a7d798e373102fe7a8a9c
History
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82303
2019-10-28 20:47:37pconnellsetnosy: + pconnell
2019-09-14 07:22:33matrixisesetpull_requests: + pull_request15747
2019-09-13 16:40:59matrixisesetnosy: + matrixise
messages: + msg352360
2019-09-13 13:15:30python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request15719
2019-09-12 10:51:41michael.foordsetmessages: + msg352101
2019-09-11 17:41:45xtreaksetmessages: + msg352000
2019-09-11 17:24:54lisroachcreate