[issue37015] Fix asyncio mock warnings

Karthikeyan Singaravelan report at bugs.python.org
Fri May 24 04:10:43 EDT 2019


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

This logic is present in create_autospec too at [0]. The behavior for patch seems to be documented at [1] which also needs to be updated to reflect that an AsyncMock by default is created instead of MagicMock if the target is an async function unlike 3.7. Probably also a versionchanged directive to note this if the current behavior is okay. Since it generates a RuntimeWarning I think it will be good if the behavior is decided before 3.8 beta 1.

> If new is omitted, then the target is replaced with a MagicMock. If patch() is used as a decorator and new is omitted, the created mock is passed in as an extra argument to the decorated function. If patch() is used as a context manager the created mock is returned by the context manager.

$ python3.7 -q
>>> import asyncio
>>> from unittest.mock import create_autospec
>>> loop = asyncio.get_event_loop()
>>> loop_mock = create_autospec(loop)
>>> loop_mock._accept_connection2
<MagicMock name='mock._accept_connection2' spec='method' id='4455012896'>

$ ./python.exe -q
>>> import asyncio
>>> from unittest.mock import create_autospec
>>> loop = asyncio.get_event_loop()
>>> loop_mock = create_autospec(loop)
>>> loop_mock._accept_connection2
<AsyncMock name='mock._accept_connection2' spec='method' id='4314046368'>

[0] https://github.com/python/cpython/blob/cccc11b38e5409861f4db345a4dd45dcc9ba470c/Lib/unittest/mock.py#L2564
[1] https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37015>
_______________________________________


More information about the Python-bugs-list mailing list