This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author penlect
Recipients asvetlov, penlect, yselivanov
Date 2021-06-13.14:52:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1623595921.99.0.250644393873.issue44410@roundup.psfhosted.org>
In-reply-to
Content
Dear maintainers,

I discovered an unexpected behavior when the `side_effect` of an `AsyncMock` includes an exception. The test case below fails but I expected it to pass:

```
import sys
import unittest
from unittest.mock import AsyncMock


class A:
    async def foobar(self):
        while True:
            try:
                return await self.mock()
            except Exception:
                continue


class TestA(unittest.IsolatedAsyncioTestCase):
    async def test_refcount(self):
        a = A()
        a.mock = AsyncMock(side_effect=[Exception(), None])
        refc = sys.getrefcount(a)
        await a.foobar()
        self.assertEqual(refc, sys.getrefcount(a))


if __name__ == "__main__":
    unittest.main()
```

If `side_effect=[Exception(), None]` is changed to `side_effect=[None, None]` the test case pass.

I discovered this in a bigger codebase while debugging why a weakref.finalize did not trigger as expected.
History
Date User Action Args
2021-06-13 14:52:02penlectsetrecipients: + penlect, asvetlov, yselivanov
2021-06-13 14:52:01penlectsetmessageid: <1623595921.99.0.250644393873.issue44410@roundup.psfhosted.org>
2021-06-13 14:52:01penlectlinkissue44410 messages
2021-06-13 14:52:01penlectcreate