@@ -54,28 +54,41 @@ def check_if_exists():
54
54
55
55
"""
56
56
57
- from __future__ import unicode_literals
57
+ from __future__ import annotations
58
58
59
59
import datetime
60
60
import functools
61
61
import logging
62
62
import random
63
+ import sys
63
64
import time
65
+ from typing import Any , Callable , TypeVar , TYPE_CHECKING
64
66
65
67
import requests .exceptions
66
68
67
69
from google .api_core import datetime_helpers
68
70
from google .api_core import exceptions
69
71
from google .auth import exceptions as auth_exceptions
70
72
73
+ if TYPE_CHECKING :
74
+ if sys .version_info >= (3 , 10 ):
75
+ from typing import ParamSpec
76
+ else :
77
+ from typing_extensions import ParamSpec
78
+
79
+ _P = ParamSpec ("_P" )
80
+ _R = TypeVar ("_R" )
81
+
71
82
_LOGGER = logging .getLogger (__name__ )
72
83
_DEFAULT_INITIAL_DELAY = 1.0 # seconds
73
84
_DEFAULT_MAXIMUM_DELAY = 60.0 # seconds
74
85
_DEFAULT_DELAY_MULTIPLIER = 2.0
75
86
_DEFAULT_DEADLINE = 60.0 * 2.0 # seconds
76
87
77
88
78
- def if_exception_type (* exception_types ):
89
+ def if_exception_type (
90
+ * exception_types : type [BaseException ],
91
+ ) -> Callable [[BaseException ], bool ]:
79
92
"""Creates a predicate to check if the exception is of a given type.
80
93
81
94
Args:
@@ -87,7 +100,7 @@ def if_exception_type(*exception_types):
87
100
exception is of the given type(s).
88
101
"""
89
102
90
- def if_exception_type_predicate (exception ) :
103
+ def if_exception_type_predicate (exception : BaseException ) -> bool :
91
104
"""Bound predicate for checking an exception type."""
92
105
return isinstance (exception , exception_types )
93
106
@@ -307,14 +320,14 @@ class Retry(object):
307
320
308
321
def __init__ (
309
322
self ,
310
- predicate = if_transient_error ,
311
- initial = _DEFAULT_INITIAL_DELAY ,
312
- maximum = _DEFAULT_MAXIMUM_DELAY ,
313
- multiplier = _DEFAULT_DELAY_MULTIPLIER ,
314
- timeout = _DEFAULT_DEADLINE ,
315
- on_error = None ,
316
- ** kwargs
317
- ):
323
+ predicate : Callable [[ BaseException ], bool ] = if_transient_error ,
324
+ initial : float = _DEFAULT_INITIAL_DELAY ,
325
+ maximum : float = _DEFAULT_MAXIMUM_DELAY ,
326
+ multiplier : float = _DEFAULT_DELAY_MULTIPLIER ,
327
+ timeout : float = _DEFAULT_DEADLINE ,
328
+ on_error : Callable [[ BaseException ], Any ] | None = None ,
329
+ ** kwargs : Any ,
330
+ ) -> None :
318
331
self ._predicate = predicate
319
332
self ._initial = initial
320
333
self ._multiplier = multiplier
@@ -323,7 +336,11 @@ def __init__(
323
336
self ._deadline = self ._timeout
324
337
self ._on_error = on_error
325
338
326
- def __call__ (self , func , on_error = None ):
339
+ def __call__ (
340
+ self ,
341
+ func : Callable [_P , _R ],
342
+ on_error : Callable [[BaseException ], Any ] | None = None ,
343
+ ) -> Callable [_P , _R ]:
327
344
"""Wrap a callable with retry behavior.
328
345
329
346
Args:
@@ -340,7 +357,7 @@ def __call__(self, func, on_error=None):
340
357
on_error = self ._on_error
341
358
342
359
@functools .wraps (func )
343
- def retry_wrapped_func (* args , ** kwargs ) :
360
+ def retry_wrapped_func (* args : _P . args , ** kwargs : _P . kwargs ) -> _R :
344
361
"""A wrapper that calls target function with retry."""
345
362
target = functools .partial (func , * args , ** kwargs )
346
363
sleep_generator = exponential_sleep_generator (
0 commit comments