Skip to content

Commit b0b44ca

Browse files
authored
Restore and deprecate removed RetryFactory methods (#84)
1 parent 834b350 commit b0b44ca

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

tests/test_retry.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66
from aiohttp.client_exceptions import ServerConnectionError
7-
from tenacity import AsyncRetrying
7+
from tenacity import AsyncRetrying, RetryCallState
88

99
from zyte_api import (
1010
AggressiveRetryFactory,
@@ -430,3 +430,33 @@ async def run():
430430
assert outcome is last_outcome # noqa: PT017
431431
else:
432432
assert not exhausted
433+
434+
435+
@pytest.mark.asyncio
436+
async def test_deprecated_temporary_download_error():
437+
class CustomRetryFactory(RetryFactory):
438+
def wait(self, retry_state: RetryCallState) -> float:
439+
self.temporary_download_error_wait(retry_state=retry_state)
440+
return 0.0
441+
442+
def stop(self, retry_state: RetryCallState) -> bool:
443+
self.temporary_download_error_stop(retry_state)
444+
return super().stop(retry_state)
445+
446+
retrying = CustomRetryFactory().build()
447+
448+
outcomes = deque((mock_request_error(status=520), None))
449+
450+
async def run():
451+
outcome = outcomes.popleft()
452+
if isinstance(outcome, Exception):
453+
raise outcome
454+
return outcome
455+
456+
run = retrying.wraps(run)
457+
with (
458+
pytest.warns(DeprecationWarning, match="temporary_download_error_stop"),
459+
pytest.warns(DeprecationWarning, match="temporary_download_error_wait"),
460+
):
461+
await run()
462+
assert not outcomes

zyte_api/_retry.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from collections import Counter
66
from datetime import timedelta
77
from itertools import count
8-
from typing import Union
8+
from typing import Callable, Union
9+
from warnings import warn
910

1011
from aiohttp import client_exceptions
1112
from tenacity import (
@@ -158,6 +159,14 @@ def _undocumented_error(exc: BaseException) -> bool:
158159
)
159160

160161

162+
def _deprecated(message: str, callable: Callable) -> Callable:
163+
def wrapper(factory, retry_state: RetryCallState):
164+
warn(message, DeprecationWarning, stacklevel=3)
165+
return callable(retry_state=retry_state)
166+
167+
return wrapper
168+
169+
161170
class RetryFactory:
162171
"""Factory class that builds the :class:`tenacity.AsyncRetrying` object
163172
that defines the :ref:`default retry policy <default-retry-policy>`.
@@ -204,14 +213,33 @@ class CustomRetryFactory(RetryFactory):
204213
)
205214

206215
# connection errors, other client and server failures
216+
network_error_stop = stop_after_uninterrupted_delay(15 * 60)
207217
network_error_wait = (
208218
# wait from 3s to ~1m
209219
wait_random(3, 7) + wait_random_exponential(multiplier=1, max=55)
210220
)
221+
222+
download_error_stop = stop_on_download_error(max_total=4, max_permanent=2)
211223
download_error_wait = network_error_wait
224+
225+
temporary_download_error_stop = _deprecated(
226+
(
227+
"The zyte_api.RetryFactory.temporary_download_error_stop() method "
228+
"is deprecated and will be removed in a future version. Use "
229+
"download_error_stop() instead."
230+
),
231+
download_error_stop,
232+
)
233+
temporary_download_error_wait = _deprecated(
234+
(
235+
"The zyte_api.RetryFactory.temporary_download_error_wait() method "
236+
"is deprecated and will be removed in a future version. Use "
237+
"download_error_wait() instead."
238+
),
239+
download_error_wait,
240+
)
241+
212242
throttling_stop = stop_never
213-
network_error_stop = stop_after_uninterrupted_delay(15 * 60)
214-
download_error_stop = stop_on_download_error(max_total=4, max_permanent=2)
215243

216244
undocumented_error_stop = stop_on_count(2)
217245
undocumented_error_wait = network_error_wait

0 commit comments

Comments
 (0)