11import socket
22from time import sleep
3+ from typing import TYPE_CHECKING , Any , Callable , Iterable , Tuple , Type , TypeVar
34
45from redis .exceptions import ConnectionError , TimeoutError
56
7+ T = TypeVar ("T" )
8+
9+ if TYPE_CHECKING :
10+ from redis .backoff import AbstractBackoff
11+
612
713class Retry :
814 """Retry a specific number of times after a failure"""
915
1016 def __init__ (
1117 self ,
12- backoff ,
13- retries ,
14- supported_errors = (ConnectionError , TimeoutError , socket .timeout ),
18+ backoff : "AbstractBackoff" ,
19+ retries : int ,
20+ supported_errors : Tuple [Type [Exception ], ...] = (
21+ ConnectionError ,
22+ TimeoutError ,
23+ socket .timeout ,
24+ ),
1525 ):
1626 """
1727 Initialize a `Retry` object with a `Backoff` object
@@ -24,15 +34,21 @@ def __init__(
2434 self ._retries = retries
2535 self ._supported_errors = supported_errors
2636
27- def update_supported_errors (self , specified_errors : list ):
37+ def update_supported_errors (
38+ self , specified_errors : Iterable [Type [Exception ]]
39+ ) -> None :
2840 """
2941 Updates the supported errors with the specified error types
3042 """
3143 self ._supported_errors = tuple (
3244 set (self ._supported_errors + tuple (specified_errors ))
3345 )
3446
35- def call_with_retry (self , do , fail ):
47+ def call_with_retry (
48+ self ,
49+ do : Callable [[], T ],
50+ fail : Callable [[Exception ], Any ],
51+ ) -> T :
3652 """
3753 Execute an operation that might fail and returns its result, or
3854 raise the exception that was thrown depending on the `Backoff` object.
0 commit comments