16
16
import logging
17
17
import re
18
18
import typing
19
- from typing import Any , Callable , Dict , Generator , Pattern
19
+ from typing import Any , Callable , Dict , Generator , Optional , Pattern
20
20
21
21
import attr
22
22
from frozendict import frozendict
@@ -110,7 +110,9 @@ def time_msec(self) -> int:
110
110
"""Returns the current system time in milliseconds since epoch."""
111
111
return int (self .time () * 1000 )
112
112
113
- def looping_call (self , f : Callable , msec : float , * args , ** kwargs ) -> LoopingCall :
113
+ def looping_call (
114
+ self , f : Callable , msec : float , * args : Any , ** kwargs : Any
115
+ ) -> LoopingCall :
114
116
"""Call a function repeatedly.
115
117
116
118
Waits `msec` initially before calling `f` for the first time.
@@ -130,20 +132,22 @@ def looping_call(self, f: Callable, msec: float, *args, **kwargs) -> LoopingCall
130
132
d .addErrback (log_failure , "Looping call died" , consumeErrors = False )
131
133
return call
132
134
133
- def call_later (self , delay , callback , * args , ** kwargs ) -> IDelayedCall :
135
+ def call_later (
136
+ self , delay : float , callback : Callable , * args : Any , ** kwargs : Any
137
+ ) -> IDelayedCall :
134
138
"""Call something later
135
139
136
140
Note that the function will be called with no logcontext, so if it is anything
137
141
other than trivial, you probably want to wrap it in run_as_background_process.
138
142
139
143
Args:
140
- delay(float) : How long to wait in seconds.
141
- callback(function) : Function to call
144
+ delay: How long to wait in seconds.
145
+ callback: Function to call
142
146
*args: Postional arguments to pass to function.
143
147
**kwargs: Key arguments to pass to function.
144
148
"""
145
149
146
- def wrapped_callback (* args , ** kwargs ) :
150
+ def wrapped_callback (* args : Any , ** kwargs : Any ) -> None :
147
151
with context .PreserveLoggingContext ():
148
152
callback (* args , ** kwargs )
149
153
@@ -158,25 +162,29 @@ def cancel_call_later(self, timer: IDelayedCall, ignore_errs: bool = False) -> N
158
162
raise
159
163
160
164
161
- def log_failure (failure , msg , consumeErrors = True ):
165
+ def log_failure (
166
+ failure : Failure , msg : str , consumeErrors : bool = True
167
+ ) -> Optional [Failure ]:
162
168
"""Creates a function suitable for passing to `Deferred.addErrback` that
163
169
logs any failures that occur.
164
170
165
171
Args:
166
- msg (str): Message to log
167
- consumeErrors (bool): If true consumes the failure, otherwise passes
168
- on down the callback chain
172
+ failure: The Failure to log
173
+ msg: Message to log
174
+ consumeErrors: If true consumes the failure, otherwise passes on down
175
+ the callback chain
169
176
170
177
Returns:
171
- func( Failure)
178
+ The Failure if consumeErrors is false. None, otherwise.
172
179
"""
173
180
174
181
logger .error (
175
- msg , exc_info = (failure .type , failure .value , failure .getTracebackObject ())
182
+ msg , exc_info = (failure .type , failure .value , failure .getTracebackObject ()) # type: ignore[arg-type]
176
183
)
177
184
178
185
if not consumeErrors :
179
186
return failure
187
+ return None
180
188
181
189
182
190
def glob_to_regex (glob : str , word_boundary : bool = False ) -> Pattern :
0 commit comments