Skip to content

gh-156460: Await async callable side effects in AsyncMock - #156465

Open
cananoo wants to merge 4 commits into
python:mainfrom
cananoo:fix/156460-asyncmock-callable
Open

gh-156460: Await async callable side effects in AsyncMock#156465
cananoo wants to merge 4 commits into
python:mainfrom
cananoo:fix/156460-asyncmock-callable

Conversation

@cananoo

@cananoo cananoo commented Aug 27, 2026

Copy link
Copy Markdown

AsyncMock did not await callable instances with an asynchronous __call__
method when they were used as side_effect. It returned the coroutine created
by the callable instead, which could result in an unawaited coroutine warning.

This change recognizes an asynchronous __call__ method and awaits the
callable result, while preserving the existing behavior for synchronous
callables and ordinary awaitable return values.

Tests:

  • ./python.exe -m test test_unittest.testmock.testasync -j 0
  • ./python.exe -m test test_unittest -j 0
  • ./python.exe Tools/patchcheck/patchcheck.py

I used OpenAI Codex to assist with investigating and preparing this change,
and reviewed the resulting code, tests, and documentation before submission.

@python-cla-bot

python-cla-bot Bot commented Aug 27, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@cananoo
cananoo marked this pull request as ready for review August 30, 2026 10:02
@cananoo
cananoo requested a review from cjw296 as a code owner August 30, 2026 10:02
Comment on lines +472 to +473
class AsyncCallable:
async def __call__(self, var):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test where you use an async mock with a sync callable object? i.e. AsyncMock + SyncCallable.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a regression test using a synchronous callable object as AsyncMock.side_effect in commit 7e15b35.

Comment on lines +1 to +2
Await async callable objects used as the ``side_effect`` of
:class:`unittest.mock.AsyncMock`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Await async callable objects used as the ``side_effect`` of
:class:`unittest.mock.AsyncMock`.
Correctly handle async callable objects used as the ``side_effect``
of :class:`unittest.mock.AsyncMock`.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied the suggested NEWS wording in commit 7e15b35.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants