Skip to content

Support spying on async callable objects - #652

Open
mehdimirzafaruk wants to merge 1 commit into
pytest-dev:mainfrom
mehdimirzafaruk:fix/async-callable-spy
Open

mehdimirzafaruk wants to merge 1 commit into
pytest-dev:mainfrom
mehdimirzafaruk:fix/async-callable-spy

Conversation

@mehdimirzafaruk

Copy link
Copy Markdown

mocker.spy recognizes coroutine functions, but not objects whose __call__ method is async. Those objects currently get a MagicMock, so calls still return an awaitable and appear to work, but await assertions such as assert_awaited_once_with are unavailable and spy_return records the coroutine rather than its resolved value.

Detect an async __call__ and use AsyncMock for that non-autospecced target. This follows unittest.mock async semantics while preserving the existing autospec path for functions and methods.

The regression test uses a class-level async callable attribute. It fails on main because the spy is a MagicMock, and passes with the fix.

Validation:

  • TestSpy: 21 passed
  • non-introspection suite: 151 passed, 1 skipped
  • pre-commit: rst, Ruff, Ruff format, mypy all passed
  • full suite has the same 7 pytest 9.1 introspection failures on both main and this branch
  • git diff --check passed

This is separate from #650, which changes context-manager warning behavior.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant