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

Commit 2607b3e

Browse files
author
David Robertson
authored
Update mypy to 0.950 and fix complaints (#12650)
1 parent c2d50e9 commit 2607b3e

File tree

10 files changed

+98
-57
lines changed

10 files changed

+98
-57
lines changed

changelog.d/12650.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update to mypy 0.950.

poetry.lock

Lines changed: 34 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ flake8-bugbear = "==21.3.2"
248248
flake8 = "*"
249249

250250
# Typechecking
251-
mypy = "==0.931"
252-
mypy-zope = "==0.3.5"
251+
mypy = "*"
252+
mypy-zope = "*"
253253
types-bleach = ">=4.1.0"
254254
types-commonmark = ">=0.9.2"
255255
types-jsonschema = ">=3.2.0"

stubs/sortedcontainers/sorteddict.pyi

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,19 @@ class SortedDict(Dict[_KT, _VT]):
8585
def popitem(self, index: int = ...) -> Tuple[_KT, _VT]: ...
8686
def peekitem(self, index: int = ...) -> Tuple[_KT, _VT]: ...
8787
def setdefault(self, key: _KT, default: Optional[_VT] = ...) -> _VT: ...
88-
@overload
89-
def update(self, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ...
90-
@overload
91-
def update(self, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
92-
@overload
93-
def update(self, **kwargs: _VT) -> None: ...
88+
# Mypy now reports the first overload as an error, because typeshed widened the type
89+
# of `__map` to its internal `_typeshed.SupportsKeysAndGetItem` type in
90+
# https://github.com/python/typeshed/pull/6653
91+
# Since sorteddicts don't change the signature of `update` from that of `dict`, we
92+
# let the stubs for `update` inherit from the stubs for `dict`. (I suspect we could
93+
# do the same for many othe methods.) We leave the stubs commented to better track
94+
# how this file has evolved from the original stubs.
95+
# @overload
96+
# def update(self, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ...
97+
# @overload
98+
# def update(self, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
99+
# @overload
100+
# def update(self, **kwargs: _VT) -> None: ...
94101
def __reduce__(
95102
self,
96103
) -> Tuple[

synapse/appservice/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Tuple
1818

1919
from prometheus_client import Counter
20+
from typing_extensions import TypeGuard
2021

2122
from synapse.api.constants import EventTypes, Membership, ThirdPartyEntityKind
2223
from synapse.api.errors import CodeMessageException
@@ -66,7 +67,7 @@ def _is_valid_3pe_metadata(info: JsonDict) -> bool:
6667
return True
6768

6869

69-
def _is_valid_3pe_result(r: JsonDict, field: str) -> bool:
70+
def _is_valid_3pe_result(r: object, field: str) -> TypeGuard[JsonDict]:
7071
if not isinstance(r, dict):
7172
return False
7273

synapse/config/appservice.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def load_appservices(
5555
) -> List[ApplicationService]:
5656
"""Returns a list of Application Services from the config files."""
5757
if not isinstance(config_files, list):
58-
logger.warning("Expected %s to be a list of AS config files.", config_files)
58+
# type-ignore: this function gets arbitrary json value; we do use this path.
59+
logger.warning("Expected %s to be a list of AS config files.", config_files) # type: ignore[unreachable]
5960
return []
6061

6162
# Dicts of value -> filename

synapse/events/presence_router.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ async def get_users_for_states(
147147
# run all the callbacks for get_users_for_states and combine the results
148148
for callback in self._get_users_for_states_callbacks:
149149
try:
150-
result = await callback(state_updates)
150+
# Note: result is an object here, because we don't trust modules to
151+
# return the types they're supposed to.
152+
result: object = await callback(state_updates)
151153
except Exception as e:
152154
logger.warning("Failed to run module API callback %s: %s", callback, e)
153155
continue

synapse/handlers/message.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ async def persist_and_notify_client_event(
14271427
# Validate a newly added alias or newly added alt_aliases.
14281428

14291429
original_alias = None
1430-
original_alt_aliases: List[str] = []
1430+
original_alt_aliases: object = []
14311431

14321432
original_event_id = event.unsigned.get("replaces_state")
14331433
if original_event_id:
@@ -1455,6 +1455,7 @@ async def persist_and_notify_client_event(
14551455
# If the old version of alt_aliases is of an unknown form,
14561456
# completely replace it.
14571457
if not isinstance(original_alt_aliases, (list, tuple)):
1458+
# TODO: check that the original_alt_aliases' entries are all strings
14581459
original_alt_aliases = []
14591460

14601461
# Check that each alias is currently valid.

synapse/metrics/background_process_metrics.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
Type,
2929
TypeVar,
3030
Union,
31-
cast,
3231
)
3332

3433
from prometheus_client import Metric
3534
from prometheus_client.core import REGISTRY, Counter, Gauge
35+
from typing_extensions import ParamSpec
3636

3737
from twisted.internet import defer
3838

@@ -256,24 +256,48 @@ async def run() -> Optional[R]:
256256
return defer.ensureDeferred(run())
257257

258258

259-
F = TypeVar("F", bound=Callable[..., Awaitable[Optional[Any]]])
259+
P = ParamSpec("P")
260260

261261

262-
def wrap_as_background_process(desc: str) -> Callable[[F], F]:
263-
"""Decorator that wraps a function that gets called as a background
264-
process.
262+
def wrap_as_background_process(
263+
desc: str,
264+
) -> Callable[
265+
[Callable[P, Awaitable[Optional[R]]]],
266+
Callable[P, "defer.Deferred[Optional[R]]"],
267+
]:
268+
"""Decorator that wraps an asynchronous function `func`, returning a synchronous
269+
decorated function. Calling the decorated version runs `func` as a background
270+
process, forwarding all arguments verbatim.
271+
272+
That is,
273+
274+
@wrap_as_background_process
275+
def func(*args): ...
276+
func(1, 2, third=3)
277+
278+
is equivalent to:
279+
280+
def func(*args): ...
281+
run_as_background_process(func, 1, 2, third=3)
265282
266-
Equivalent to calling the function with `run_as_background_process`
283+
The former can be convenient if `func` needs to be run as a background process in
284+
multiple places.
267285
"""
268286

269-
def wrap_as_background_process_inner(func: F) -> F:
287+
def wrap_as_background_process_inner(
288+
func: Callable[P, Awaitable[Optional[R]]]
289+
) -> Callable[P, "defer.Deferred[Optional[R]]"]:
270290
@wraps(func)
271291
def wrap_as_background_process_inner_2(
272-
*args: Any, **kwargs: Any
292+
*args: P.args, **kwargs: P.kwargs
273293
) -> "defer.Deferred[Optional[R]]":
274-
return run_as_background_process(desc, func, *args, **kwargs)
294+
# type-ignore: mypy is confusing kwargs with the bg_start_span kwarg.
295+
# Argument 4 to "run_as_background_process" has incompatible type
296+
# "**P.kwargs"; expected "bool"
297+
# See https://github.com/python/mypy/issues/8862
298+
return run_as_background_process(desc, func, *args, **kwargs) # type: ignore[arg-type]
275299

276-
return cast(F, wrap_as_background_process_inner_2)
300+
return wrap_as_background_process_inner_2
277301

278302
return wrap_as_background_process_inner
279303

tests/storage/test_monthly_active_users.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,19 +222,19 @@ def test_reap_monthly_active_users_reserved_users(self):
222222
self.store.user_add_threepid(user, "email", email, now, now)
223223
)
224224

225-
d = self.store.db_pool.runInteraction(
226-
"initialise", self.store._initialise_reserved_users, threepids
225+
self.get_success(
226+
self.store.db_pool.runInteraction(
227+
"initialise", self.store._initialise_reserved_users, threepids
228+
)
227229
)
228-
self.get_success(d)
229230

230231
count = self.get_success(self.store.get_monthly_active_count())
231232
self.assertEqual(count, initial_users)
232233

233234
users = self.get_success(self.store.get_registered_reserved_users())
234235
self.assertEqual(len(users), reserved_user_number)
235236

236-
d = self.store.reap_monthly_active_users()
237-
self.get_success(d)
237+
self.get_success(self.store.reap_monthly_active_users())
238238

239239
count = self.get_success(self.store.get_monthly_active_count())
240240
self.assertEqual(count, self.hs.config.server.max_mau_value)

0 commit comments

Comments
 (0)