Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tenacity/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def __init__(
super().__init__(self._check)

def _check(self, e: BaseException) -> bool:
return not isinstance(e, self.exception_types)
return isinstance(e, Exception) and not isinstance(e, self.exception_types)


class retry_unless_exception_type(retry_if_exception):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,28 @@ def after(retry_state: RetryCallState) -> None:
assert len(set(things)) == 1
assert list(attempt_nos2) == [1, 2, 3]

@asynctest
async def test_retry_if_not_exception_type_does_not_swallow_cancelled_error(
self,
) -> None:
attempts = 0

@retry(
retry=tenacity.retry_if_not_exception_type(ValueError),
stop=stop_after_attempt(2),
wait=wait_fixed(0),
reraise=True,
)
async def always_sleep() -> None:
nonlocal attempts
attempts += 1
await asyncio.sleep(1)

with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(always_sleep(), 0.01)

assert attempts == 1


class TestAsyncEnabled(unittest.TestCase):
@asynctest
Expand Down
21 changes: 21 additions & 0 deletions tests/test_tenacity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,27 @@ def test_retry_except_exception_of_type(self) -> None:
self.assertTrue(isinstance(err, IOError))
print(err)

def test_retry_except_exception_of_type_does_not_retry_base_exception(self) -> None:
calls = 0

class CustomBaseError(BaseException):
pass

@retry(
stop=tenacity.stop_after_attempt(3),
retry=tenacity.retry_if_not_exception_type(IOError),
reraise=True,
)
def raises_base_exception() -> None:
nonlocal calls
calls += 1
raise CustomBaseError

with pytest.raises(CustomBaseError):
raises_base_exception()

assert calls == 1

def test_retry_until_exception_of_type_attempt_number(self) -> None:
try:
self.assertTrue(
Expand Down