-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Refactor AsyncMock setup logic in create_autospec #81228
Comments
import asyncio
from unittest.mock import create_autospec
async def foo(): pass
spec = create_autospec(foo)
awaitable = spec()
async def main(): await awaitable
asyncio.run(main()) Traceback (most recent call last):
File "/tmp/spam.py", line 13, in <module>
asyncio.run(main())
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/asyncio/base_events.py", line 614, in run_until_complete
return future.result()
File "/tmp/spam.py", line 11, in main
await awaitable
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 2045, in _mock_call
return await proxy()
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 2043, in proxy
await self.awaited._notify()
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 596, in __getattr__
raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute 'awaited'
>>> spec.assert_awaited_once_with(1) # Due to late binding assert_not_awaited is always called
TypeError: assert_not_awaited() takes 1 positional argument but 2 were given
>>> spec.assert_not_awaited()
AssertionError: Expected mock to have been awaited once. Awaited 1 times.
I have a PR with the above changes that I will post shortly for review. [0] Line 2506 in 7114c65
[1] Line 2518 in 7114c65
[2] Line 2154 in 7114c65
[3] https://docs.python.org/3.8/library/unittest.mock.html#mocking-magic-methods |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: