25
25
import time
26
26
27
27
from enum import Enum
28
- from typing import Any , Callable , TYPE_CHECKING
28
+ from typing import Any , Callable , Optional , TYPE_CHECKING
29
29
30
30
import requests .exceptions
31
31
@@ -238,8 +238,8 @@ def __init__(
238
238
initial : float = _DEFAULT_INITIAL_DELAY ,
239
239
maximum : float = _DEFAULT_MAXIMUM_DELAY ,
240
240
multiplier : float = _DEFAULT_DELAY_MULTIPLIER ,
241
- timeout : float = _DEFAULT_DEADLINE ,
242
- on_error : Callable [[Exception ], Any ] | None = None ,
241
+ timeout : Optional [ float ] = _DEFAULT_DEADLINE ,
242
+ on_error : Optional [ Callable [[Exception ], Any ]] = None ,
243
243
** kwargs : Any ,
244
244
) -> None :
245
245
self ._predicate = predicate
@@ -265,48 +265,39 @@ def deadline(self) -> float | None:
265
265
def timeout (self ) -> float | None :
266
266
return self ._timeout
267
267
268
- def _replace (
269
- self ,
270
- predicate : Callable [[Exception ], bool ] | None = None ,
271
- initial : float | None = None ,
272
- maximum : float | None = None ,
273
- multiplier : float | None = None ,
274
- timeout : float | None = None ,
275
- on_error : Callable [[Exception ], Any ] | None = None ,
276
- ) -> Self :
277
- return type (self )(
278
- predicate = predicate or self ._predicate ,
279
- initial = initial or self ._initial ,
280
- maximum = maximum or self ._maximum ,
281
- multiplier = multiplier or self ._multiplier ,
282
- timeout = timeout or self ._timeout ,
283
- on_error = on_error or self ._on_error ,
284
- )
285
-
286
268
def with_deadline (self , deadline : float | None ) -> Self :
287
269
"""Return a copy of this retry with the given timeout.
288
270
289
271
DEPRECATED: use :meth:`with_timeout` instead. Refer to the ``Retry`` class
290
272
documentation for details.
291
273
292
274
Args:
293
- deadline (float): How long to keep retrying, in seconds.
275
+ deadline (float|None): How long to keep retrying, in seconds. If None,
276
+ no timeout is enforced.
294
277
295
278
Returns:
296
279
Retry: A new retry instance with the given timeout.
297
280
"""
298
- return self ._replace ( timeout = deadline )
281
+ return self .with_timeout ( deadline )
299
282
300
- def with_timeout (self , timeout : float ) -> Self :
283
+ def with_timeout (self , timeout : float | None ) -> Self :
301
284
"""Return a copy of this retry with the given timeout.
302
285
303
286
Args:
304
- timeout (float): How long to keep retrying, in seconds.
287
+ timeout (float): How long to keep retrying, in seconds. If None,
288
+ no timeout will be enforced.
305
289
306
290
Returns:
307
291
Retry: A new retry instance with the given timeout.
308
292
"""
309
- return self ._replace (timeout = timeout )
293
+ return type (self )(
294
+ predicate = self ._predicate ,
295
+ initial = self ._initial ,
296
+ maximum = self ._maximum ,
297
+ multiplier = self ._multiplier ,
298
+ timeout = timeout ,
299
+ on_error = self ._on_error ,
300
+ )
310
301
311
302
def with_predicate (self , predicate : Callable [[Exception ], bool ]) -> Self :
312
303
"""Return a copy of this retry with the given predicate.
@@ -318,26 +309,42 @@ def with_predicate(self, predicate: Callable[[Exception], bool]) -> Self:
318
309
Returns:
319
310
Retry: A new retry instance with the given predicate.
320
311
"""
321
- return self ._replace (predicate = predicate )
312
+ return type (self )(
313
+ predicate = predicate ,
314
+ initial = self ._initial ,
315
+ maximum = self ._maximum ,
316
+ multiplier = self ._multiplier ,
317
+ timeout = self ._timeout ,
318
+ on_error = self ._on_error ,
319
+ )
322
320
323
321
def with_delay (
324
322
self ,
325
- initial : float | None = None ,
326
- maximum : float | None = None ,
327
- multiplier : float | None = None ,
323
+ initial : Optional [ float ] = None ,
324
+ maximum : Optional [ float ] = None ,
325
+ multiplier : Optional [ float ] = None ,
328
326
) -> Self :
329
327
"""Return a copy of this retry with the given delay options.
330
328
331
329
Args:
332
330
initial (float): The minimum amount of time to delay (in seconds). This must
333
- be greater than 0.
334
- maximum (float): The maximum amount of time to delay (in seconds).
335
- multiplier (float): The multiplier applied to the delay.
331
+ be greater than 0. If None, the current value is used.
332
+ maximum (float): The maximum amount of time to delay (in seconds). If None, the
333
+ current value is used.
334
+ multiplier (float): The multiplier applied to the delay. If None, the current
335
+ value is used.
336
336
337
337
Returns:
338
- Retry: A new retry instance with the given predicate .
338
+ Retry: A new retry instance with the given delay options .
339
339
"""
340
- return self ._replace (initial = initial , maximum = maximum , multiplier = multiplier )
340
+ return type (self )(
341
+ predicate = self ._predicate ,
342
+ initial = initial if initial is not None else self ._initial ,
343
+ maximum = maximum if maximum is not None else self ._maximum ,
344
+ multiplier = multiplier if multiplier is not None else self ._multiplier ,
345
+ timeout = self ._timeout ,
346
+ on_error = self ._on_error ,
347
+ )
341
348
342
349
def __str__ (self ) -> str :
343
350
return (
0 commit comments