From 6b715af1f9d5cfef2c496a05b0eefa4ebeab21a7 Mon Sep 17 00:00:00 2001 From: CyberneticX-Tech <138270143+CyberneticX-Tech@users.noreply.github.com> Date: Sun, 6 Sep 2026 23:26:16 -0700 Subject: [PATCH] fix(plugin): ensure mocker.stub is not detected as a coroutine function (#375) --- src/pytest_mock/plugin.py | 8 ++++---- tests/test_pytest_mock.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/pytest_mock/plugin.py b/src/pytest_mock/plugin.py index df63fb4..7bfa214 100644 --- a/src/pytest_mock/plugin.py +++ b/src/pytest_mock/plugin.py @@ -235,10 +235,10 @@ def stub(self, name: str | None = None) -> unittest.mock.MagicMock: :param name: the constructed stub's name as used in repr :return: Stub object. """ - return cast( - unittest.mock.MagicMock, - self.mock_module.MagicMock(spec=lambda *args, **kwargs: None, name=name), - ) + fn = lambda *args, **kwargs: None + stub = self.mock_module.MagicMock(spec=fn, name=name) + stub.__code__ = fn.__code__ + return cast(unittest.mock.MagicMock, stub) def async_stub(self, name: str | None = None) -> AsyncMockType: """ diff --git a/tests/test_pytest_mock.py b/tests/test_pytest_mock.py index c497aea..9ada294 100644 --- a/tests/test_pytest_mock.py +++ b/tests/test_pytest_mock.py @@ -260,6 +260,18 @@ def test_failure_message_with_name(self, mocker: MagicMock, name: str) -> None: def test_async_stub_type(self, mocker: MockerFixture) -> None: assert isinstance(mocker.async_stub(), AsyncMock) + def test_not_coroutine_function(self, mocker: MockerFixture) -> None: + import inspect + + stub = mocker.stub() + assert not inspect.iscoroutinefunction(stub) + + def test_async_stub_is_coroutine_function(self, mocker: MockerFixture) -> None: + import inspect + + async_stub = mocker.async_stub() + assert inspect.iscoroutinefunction(async_stub) + def test_instance_method_spy(mocker: MockerFixture) -> None: class Foo: