New submission from Thomas Grainger <[email protected]>:
aentering a MagicMock() results in an AsyncMock which behaves differently than
I expected:
```
python3.9 -m asyncio
asyncio REPL 3.9.0a5 (default, Apr 18 2020, 00:00:31)
[GCC 9.3.0] on linux
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> from unittest import mock
>>> with mock.MagicMock() as m:
... with m.foo() as u:
... u.hello()
...
<MagicMock name='mock.__enter__().foo().__enter__().hello()'
id='140670894620048'>
>>> async with mock.MagicMock() as m:
... async with m.foo() as u:
... u.hello()
...
<console>:2: RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was
never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
File "/usr/lib/python3.9/concurrent/futures/_base.py", line 439, in result
return self.__get_result()
File "/usr/lib/python3.9/concurrent/futures/_base.py", line 388, in
__get_result
raise self._exception
File "<console>", line 2, in <module>
AttributeError: __aenter__
```
This is annoying for mocking database interfaces like
```
async def update_users(db, user):
async with db.connection() as conn:
async with conn.transaction() as tx:
...
```
----------
components: Tests, asyncio
messages: 367419
nosy: asvetlov, graingert, yselivanov
priority: normal
severity: normal
status: open
title: MagicMock __aenter__ should be AsyncMock(return_value=MagicMock())
versions: Python 3.8, Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue40406>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com