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
AsyncMock issue with awaitable return_value/side_effect/wraps #83038
Comments
If you are trying to use AsyncMock to mock a coroutine that returns awaitable objects, AsyncMock awaits on those objects instead of returning them as is. Example: Expected: This problem affects side_effects and wraps. |
I think this deserves discussion :) On one hand, it's a welcome change, on another it's kind of a regression. Up until 3.8, our tests used to look like this: --- async def foo():
return await bar() # test async def helper(value):
return value
async def test_foo():
with patch("bar", return_value=helper(42)):
assert await foo() == 42 I feel that the default class At the same time, So, on one hand, it's a bit of a regression, but on the other, looking ahead, I would really like Can we have it both ways? If not, what way is a better way? |
Its not possible to have it both ways. Also it stinks too much of trying to guess. The root of your issue is you want a normal MagicMock not an AsyncMock. Its the automatic behavior of patch to pick AsyncMock vs MagicMock that is the heart of your issue. This bug fix doesn't involve that behavior at all, and AsyncMock was measurably broken without the fix, in an unavoidable way. Your breakage is avoidable by changes to how you patch. with patch("bar", return_value=42) To still do it the old way you would have to pass new_callable=MagicMock to patch. |
Thank you for explanation, Jason! I guess that the bug report and the patch were too technical for me to understand 😅 I'm happy with the behaviour in Python 3.8.1 and now I know it's going to stay, I'll just change the tests in our code base. |
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: