bpo-37015: Ensure tasks created by _accept_connection2 due to AsyncMock are completed by tirkarthi · Pull Request #13661 · python/cpython
From 3.8 async functions used with mock.patch return an AsyncMock. _accept_connection2 is an async function where create_task is also mocked. Don't mock create_task so that tasks are created out of coroutine returned by AsyncMock and the tasks are completed.
The other way to solve this would be to remove mock.patch for create_task so that _accept_connection2 returns an AsyncMock and self.create_task creates a task from the coroutine. This would require a call like loop.run_until_complete(asyncio.sleep(0)) to ensure the task is completed I guess. I prefer this since this just restores 3.7 behavior without additional code change but I can change the approach if needed.
Command to reproduce the warnings :
./python.exe -Wall -X tracemalloc -m unittest -vv test.test_asyncio.test_selector_events.BaseSelectorEventLoopTests.test_accept_connection_multiple
| mock_obj = mock.patch.object | ||
| with mock_obj(self.loop, '_accept_connection2') as accept2_mock: | ||
| with mock_obj(self.loop, '_accept_connection2', | ||
| new=mock.MagicMock()) as accept2_mock: |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! I usually use new for created objects and new_callable when I want a different mock type.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change in behavior has to be documented as noted in the tracker. Currently there is below text where MagicMock is documented as the target type but that changed in 3.8 with AsyncMock for async functions and MagicMock for normal functions when new is omitted in mock.patch.
https://docs.python.org/3/library/unittest.mock.html#patch
If new is omitted, then the target is replaced with a MagicMock
The other way to solve this would be to remove mock.patch for
create_task
Please do it. Double mocking for both _accept_connection2 and create_task looks redundant.
P.S.
Thank you very much for taking care of tests.
I'm pretty busy now and very appreciate your help with solving non-urgent but extremely important tasks,
You're welcome @asvetlov . I have made the requested changes to make sure only _accept_connection2 is mocked and the tasks are completed. My assumption is that create_task creates the task but doesn't execute them and using asyncio.sleep(0) there is a control switch to the created tasks without sleeping and they are completed. I have added a comment. Feel free to add suggestions if I have misunderstood something.
Thanks
tirkarthi
changed the title
bpo-37015: Use MagicMock for _accept_connection2 which is an async function not awaited
bpo-37015: Ensure tasks created by _accept_connection2 due to AsyncMock are completed
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
DinoV pushed a commit to DinoV/cpython that referenced this pull request
…ck are completed (pythonGH-13661) From 3.8 async functions used with mock.patch return an `AsyncMock`. `_accept_connection2` is an async function where create_task is also mocked. Don't mock `create_task` so that tasks are created out of coroutine returned by `AsyncMock` and the tasks are completed. https://bugs.python.org/issue37015