Incorrect exception raised with two retrying blocks in function
Author: paulmdaviesCreated Sep 1, 2025Updated Aug 13, 2026
Caveats
I've tried to work out what's going on here with Googling, but I've been unsuccessful. I think this is a bug, but appreciate it could be either
- Expected behaviour
- Unsupported use case
It's also unusual for me to use two seperate retrying blocks in the same test case, but the test I'm writing is under development and one will be removed later.
Minimal Example
from unittest import TestCase
from unittest.mock import MagicMock
from tenacity import Retrying, stop_after_attempt
class TestTenacity(TestCase):
def setUp(self):
self.foo = MagicMock(side_effect=[2, 1])
def test_tenacity(self):
for attempt in Retrying(stop=stop_after_attempt(2)):
with attempt:
self.assertEqual(1, self.foo())
for attempt in Retrying(stop=stop_after_attempt(2)):
with attempt:
raise Exception('bippy')Flow
- retry 0 attempt 0 (
r0a0) raises - retry 0 attempt 1 (
r0a1) does not raise - retry 1 attempt 0 (
r1a0) raises
Expectation
We should see the exception r1a0 raised, and the unittest comparison should not appear, e.g.
Reality
The correct exception is raised, but the previous exception has been rendered by unittest comparison, which is very confusing.
Source: jd/tenacity