Skip to content

fix(plugin): ensure mocker.stub is not detected as a coroutine function - #607

Open
CyberneticX-Tech wants to merge 1 commit into
pytest-dev:mainfrom
CyberneticX-Tech:fix/stub-not-coroutine-function
Open

fix(plugin): ensure mocker.stub is not detected as a coroutine function#607
CyberneticX-Tech wants to merge 1 commit into
pytest-dev:mainfrom
CyberneticX-Tech:fix/stub-not-coroutine-function

Conversation

@CyberneticX-Tech

Copy link
Copy Markdown

Resolves #375

Because mocker.stub() constructs a MagicMock(spec=lambda *args, **kwargs: None), isinstance(stub, types.FunctionType) evaluates to True. When inspect.iscoroutinefunction(stub) is called, it inspects stub.__code__.co_flags & CO_COROUTINE. Accessing stub.__code__ creates a child MagicMock, whose bitwise & operation also returns a MagicMock, which evaluates to truthy, incorrectly marking the synchronous stub as a coroutine function.

Assigning fn.__code__ to stub.__code__ ensures stub retains the real code flags of a standard synchronous function while preserving all other MagicMock behaviors.

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.

mocker.stub() is a coroutine function

1 participant