Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit b64b6d1

Browse files
authored
Add more type hints to synapse.util. (#11321)
1 parent 2fffcb2 commit b64b6d1

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

changelog.d/11321.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add type hints to `synapse.util`.

synapse/http/server.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def return_json_error(f: failure.Failure, request: SynapseRequest) -> None:
9898
"Failed handle request via %r: %r",
9999
request.request_metrics.name,
100100
request,
101-
exc_info=(f.type, f.value, f.getTracebackObject()), # type: ignore
101+
exc_info=(f.type, f.value, f.getTracebackObject()), # type: ignore[arg-type]
102102
)
103103

104104
# Only respond with an error response if we haven't already started writing,
@@ -150,7 +150,7 @@ def return_html_error(
150150
logger.error(
151151
"Failed handle request %r",
152152
request,
153-
exc_info=(f.type, f.value, f.getTracebackObject()), # type: ignore
153+
exc_info=(f.type, f.value, f.getTracebackObject()), # type: ignore[arg-type]
154154
)
155155
else:
156156
code = HTTPStatus.INTERNAL_SERVER_ERROR
@@ -159,7 +159,7 @@ def return_html_error(
159159
logger.error(
160160
"Failed handle request %r",
161161
request,
162-
exc_info=(f.type, f.value, f.getTracebackObject()), # type: ignore
162+
exc_info=(f.type, f.value, f.getTracebackObject()), # type: ignore[arg-type]
163163
)
164164

165165
if isinstance(error_template, str):

synapse/util/__init__.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import logging
1717
import re
1818
import typing
19-
from typing import Any, Callable, Dict, Generator, Pattern
19+
from typing import Any, Callable, Dict, Generator, Optional, Pattern
2020

2121
import attr
2222
from frozendict import frozendict
@@ -110,7 +110,9 @@ def time_msec(self) -> int:
110110
"""Returns the current system time in milliseconds since epoch."""
111111
return int(self.time() * 1000)
112112

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:
114116
"""Call a function repeatedly.
115117
116118
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
130132
d.addErrback(log_failure, "Looping call died", consumeErrors=False)
131133
return call
132134

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:
134138
"""Call something later
135139
136140
Note that the function will be called with no logcontext, so if it is anything
137141
other than trivial, you probably want to wrap it in run_as_background_process.
138142
139143
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
142146
*args: Postional arguments to pass to function.
143147
**kwargs: Key arguments to pass to function.
144148
"""
145149

146-
def wrapped_callback(*args, **kwargs):
150+
def wrapped_callback(*args: Any, **kwargs: Any) -> None:
147151
with context.PreserveLoggingContext():
148152
callback(*args, **kwargs)
149153

@@ -158,25 +162,29 @@ def cancel_call_later(self, timer: IDelayedCall, ignore_errs: bool = False) -> N
158162
raise
159163

160164

161-
def log_failure(failure, msg, consumeErrors=True):
165+
def log_failure(
166+
failure: Failure, msg: str, consumeErrors: bool = True
167+
) -> Optional[Failure]:
162168
"""Creates a function suitable for passing to `Deferred.addErrback` that
163169
logs any failures that occur.
164170
165171
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
169176
170177
Returns:
171-
func(Failure)
178+
The Failure if consumeErrors is false. None, otherwise.
172179
"""
173180

174181
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]
176183
)
177184

178185
if not consumeErrors:
179186
return failure
187+
return None
180188

181189

182190
def glob_to_regex(glob: str, word_boundary: bool = False) -> Pattern:

0 commit comments

Comments
 (0)