-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Fix RuntimeWarning in unittest.mock asyncio example #13449
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
Conversation
cc: @lisroach I couldn't find a better way to suppress the RuntimeWarning than using the object in the example. Feel free to suggest if there is a better method. Sample warning : https://travis-ci.org/python/cpython/jobs/534904298#L1553 |
thanks! |
I removed automerge label. |
@asvetlov There is one more https://travis-ci.org/python/cpython/jobs/534932562#L1564 . I am not sure why my local build and CI doesn't show all the warnings initially. |
I went ahead and removed the warning line in current CI and I get below one. For each line I remove, the subsequent line that accesses
|
I have removed another call by guessing at 7ece18f and there is no RuntimeWarning now in CI. But removing it seems incorrect because as per the section it's there to illustrate that setting spec on Mock/MagicMock will return coroutine object on calling the object. Adding |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Hmm weird, I hadn't seen these warnings when I ran locally. Thanks for digging into it @tirkarthi! I definitely wish we could avoid cluttering the simple examples. I'll try to take a look tonight but no solution off the top of my head. |
A simplified version of the changes. The foo.rst >>> import asyncio
>>> import inspect
>>> from unittest.mock import AsyncMock, MagicMock
>>> mock = AsyncMock()
>>> asyncio.iscoroutinefunction(mock)
True
>>> inspect.isawaitable(mock())
True
>>> async def async_func(): pass
...
>>> mock = MagicMock(async_func)
>>> mock # doctest: +ELLIPSIS
<MagicMock spec='function' id='...'>
>>> mock() # doctest: +ELLIPSIS
<coroutine object AsyncMockMixin._mock_call at ...>
|
Dropping doctests for async mocks is another option. |
Dropping doctest is a good idea. I have added |
thanks! |
Not quite sure I understand why. Do you mean you want Another option is to set |
Yes, I mean after #13148 landing there is a possibility for extending doctest to support asyncio. |
RuntimeWarning
ininspect.isawaitable(mock())
wheremock()
was not awaited.