Fix a test failure specifically in PyPy 3.11.15+#1310
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
When converted to errors:
As found manually, it fails on accessing the
coro.cr_frameattribute 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 thecr_…attributes in addition toclose(), 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 thecororemains unawaited and therefore issues a warning on garbage collection.The extra line also fixes an issue when
corowas still referenced by the mock and not garbage-collected (a pre-existing bug in the test).