Skip to content

Fix a test failure specifically in PyPy 3.11.15+#1310

Merged
nolar merged 1 commit into
mainfrom
fix-pypy-3.11.15-async-mock
Jun 3, 2026
Merged

Fix a test failure specifically in PyPy 3.11.15+#1310
nolar merged 1 commit into
mainfrom
fix-pypy-3.11.15-async-mock

Conversation

@nolar

@nolar nolar commented Jun 3, 2026

Copy link
Copy Markdown
Owner

It works in CPython (all versions). It works in PyPy 3.11.13. Only PyPy 3.11.15 has this problem.

The coro cancellation test fails with an existing warning while no warnings are expected. The warning is:

{message : RuntimeWarning("coroutine 'f' was never awaited"), category : 'RuntimeWarning', filename : '/Users/nolar/.pyenv/versions/pypy3.11-7.3.22/lib/pypy3.11/unittest/mock.py', lineno : 457, line : None}

When converted to errors:

    def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
                       _eat_self=False):
        ………
        for attr in dir(spec):
>           if iscoroutinefunction(getattr(spec, attr, None)):
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^
E           RuntimeWarning: coroutine 'f' was never awaited

As found manually, it fails on accessing the coro.cr_frame attribute while enumerating it — just accessing, not doing anything with it.

Removing the spec part of the async mock helps. It was only needed to ensure that coro.close() does not exist. Alternative: enumerate ourselves (fields = dir(coro)) and exclude the cr_… attributes in addition to close(), but also some other unsupported magic methods as per Mock/AsyncMock docs — I do not know which ones.

An artificial test to ensure the test fails when the bug is reproduced: in cancel_coro(), comment out the task creation and awaiting, in which case the coro remains unawaited and therefore issues a warning on garbage collection.

The extra line also fixes an issue when coro was still referenced by the mock and not garbage-collected (a pre-existing bug in the test).

It works in CPython (all versions). It works in PyPy 3.11.13. Only PyPy 3.11.15 has this problem.

The coro cancellation test fails with an existing warning while no warnings are expected. The warning is:

```
{message : RuntimeWarning("coroutine 'f' was never awaited"), category : 'RuntimeWarning', filename : '/Users/nolar/.pyenv/versions/pypy3.11-7.3.22/lib/pypy3.11/unittest/mock.py', lineno : 457, line : None}
```

When converted to errors:

```
    def _mock_add_spec(self, spec, spec_set, _spec_as_instance=False,
                       _eat_self=False):
        ………
        for attr in dir(spec):
>           if iscoroutinefunction(getattr(spec, attr, None)):
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^
E           RuntimeWarning: coroutine 'f' was never awaited
```

As found manually, it fails on accessing the `coro.cr_frame` attribute while enumerating it — just accessing, not doing anything with it.

Removing the spec part of the async mock helps. It was only needed to ensure that `coro.close()` does not exist. Alternative: enumerate ourselves (`fields = dir(coro)`) and exclude the `cr_…` attributes in addition to `close()`, but also some other unsupported magic methods as per Mock/AsyncMock docs — I do not know which ones.

An artificial test to ensure the test fails when the bug is reproduced: in `cancel_coro()`, comment out the task creation and awaiting, in which case the `coro` remains unawaited and therefore issues a warning on garbage collection.

The extra line also fixes an issue when `coro` was still referenced by the mock and not garbage-collected (a pre-existing bug in the test).

Signed-off-by: Sergey Vasilyev <nolar@nolar.info>
@nolar nolar added bug Something isn't working automation CI/CD: testing, linting, releasing automatically labels Jun 3, 2026
@nolar nolar merged commit b768d24 into main Jun 3, 2026
25 checks passed
@nolar nolar deleted the fix-pypy-3.11.15-async-mock branch June 3, 2026 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automation CI/CD: testing, linting, releasing automatically bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant