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

Commit 255860b

Browse files
committed
Merge commit '208e1d3eb' into anoa/dinsic_release_1_21_x
* commit '208e1d3eb': Fix typing for `@cached` wrapped functions (#8240) Remove useless changelog about reverting a #8239. Revert pinning of setuptools (#8239) Fix typing for SyncHandler (#8237) wrap `_get_e2e_device_keys_and_signatures_txn` in a non-txn method (#8231) Add an overload for simple_select_one_onecol_txn. (#8235)
2 parents 2df215d + 208e1d3 commit 255860b

File tree

17 files changed

+200
-53
lines changed

17 files changed

+200
-53
lines changed

INSTALL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ mkdir -p ~/synapse
7373
virtualenv -p python3 ~/synapse/env
7474
source ~/synapse/env/bin/activate
7575
pip install --upgrade pip
76-
pip install --upgrade setuptools!=50.0 # setuptools==50.0 fails on some older Python versions
76+
pip install --upgrade setuptools
7777
pip install matrix-synapse
7878
```
7979

changelog.d/8212.bugfix

Lines changed: 0 additions & 1 deletion
This file was deleted.

changelog.d/8231.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor queries for device keys and cross-signatures.

changelog.d/8235.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add type hints to `StreamStore`.

changelog.d/8237.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix type hints in `SyncHandler`.

changelog.d/8240.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix type hints for functions decorated with `@cached`.

mypy.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[mypy]
22
namespace_packages = True
3-
plugins = mypy_zope:plugin
3+
plugins = mypy_zope:plugin, scripts-dev/mypy_synapse_plugin.py
44
follow_imports = silent
55
check_untyped_defs = True
66
show_error_codes = True
@@ -51,6 +51,7 @@ files =
5151
synapse/storage/util,
5252
synapse/streams,
5353
synapse/types.py,
54+
synapse/util/caches/descriptors.py,
5455
synapse/util/caches/stream_change_cache.py,
5556
synapse/util/metrics.py,
5657
tests/replication,

scripts-dev/mypy_synapse_plugin.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2020 The Matrix.org Foundation C.I.C.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""This is a mypy plugin for Synpase to deal with some of the funky typing that
17+
can crop up, e.g the cache descriptors.
18+
"""
19+
20+
from typing import Callable, Optional
21+
22+
from mypy.plugin import MethodSigContext, Plugin
23+
from mypy.typeops import bind_self
24+
from mypy.types import CallableType
25+
26+
27+
class SynapsePlugin(Plugin):
28+
def get_method_signature_hook(
29+
self, fullname: str
30+
) -> Optional[Callable[[MethodSigContext], CallableType]]:
31+
if fullname.startswith(
32+
"synapse.util.caches.descriptors._CachedFunction.__call__"
33+
):
34+
return cached_function_method_signature
35+
return None
36+
37+
38+
def cached_function_method_signature(ctx: MethodSigContext) -> CallableType:
39+
"""Fixes the `_CachedFunction.__call__` signature to be correct.
40+
41+
It already has *almost* the correct signature, except:
42+
43+
1. the `self` argument needs to be marked as "bound"; and
44+
2. any `cache_context` argument should be removed.
45+
"""
46+
47+
# First we mark this as a bound function signature.
48+
signature = bind_self(ctx.default_signature)
49+
50+
# Secondly, we remove any "cache_context" args.
51+
#
52+
# Note: We should be only doing this if `cache_context=True` is set, but if
53+
# it isn't then the code will raise an exception when its called anyway, so
54+
# its not the end of the world.
55+
context_arg_index = None
56+
for idx, name in enumerate(signature.arg_names):
57+
if name == "cache_context":
58+
context_arg_index = idx
59+
break
60+
61+
if context_arg_index:
62+
arg_types = list(signature.arg_types)
63+
arg_types.pop(context_arg_index)
64+
65+
arg_names = list(signature.arg_names)
66+
arg_names.pop(context_arg_index)
67+
68+
arg_kinds = list(signature.arg_kinds)
69+
arg_kinds.pop(context_arg_index)
70+
71+
signature = signature.copy_modified(
72+
arg_types=arg_types, arg_names=arg_names, arg_kinds=arg_kinds,
73+
)
74+
75+
return signature
76+
77+
78+
def plugin(version: str):
79+
# This is the entry point of the plugin, and let's us deal with the fact
80+
# that the mypy plugin interface is *not* stable by looking at the version
81+
# string.
82+
#
83+
# However, since we pin the version of mypy Synapse uses in CI, we don't
84+
# really care.
85+
return SynapsePlugin

synapse/handlers/federation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,11 @@ async def _get_missing_events_for_pdu(self, origin, pdu, prevs, min_depth):
443443
if not prevs - seen:
444444
return
445445

446-
latest = await self.store.get_latest_event_ids_in_room(room_id)
446+
latest_list = await self.store.get_latest_event_ids_in_room(room_id)
447447

448448
# We add the prev events that we have seen to the latest
449449
# list to ensure the remote server doesn't give them to us
450-
latest = set(latest)
450+
latest = set(latest_list)
451451
latest |= seen
452452

453453
logger.info(
@@ -784,7 +784,7 @@ async def _process_received_pdu(
784784
# keys across all devices.
785785
current_keys = [
786786
key
787-
for device in cached_devices
787+
for device in cached_devices.values()
788788
for key in device.get("keys", {}).get("keys", {}).values()
789789
]
790790

@@ -2129,8 +2129,8 @@ async def _check_for_soft_fail(
21292129
if backfilled or event.internal_metadata.is_outlier():
21302130
return
21312131

2132-
extrem_ids = await self.store.get_latest_event_ids_in_room(event.room_id)
2133-
extrem_ids = set(extrem_ids)
2132+
extrem_ids_list = await self.store.get_latest_event_ids_in_room(event.room_id)
2133+
extrem_ids = set(extrem_ids_list)
21342134
prev_event_ids = set(event.prev_event_ids())
21352135

21362136
if extrem_ids == prev_event_ids:

synapse/handlers/sync.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import itertools
1818
import logging
19-
from typing import Any, Dict, FrozenSet, List, Optional, Set, Tuple
19+
from typing import TYPE_CHECKING, Any, Dict, FrozenSet, List, Optional, Set, Tuple
2020

2121
import attr
2222
from prometheus_client import Counter
@@ -44,6 +44,9 @@
4444
from synapse.util.metrics import Measure, measure_func
4545
from synapse.visibility import filter_events_for_client
4646

47+
if TYPE_CHECKING:
48+
from synapse.server import HomeServer
49+
4750
logger = logging.getLogger(__name__)
4851

4952
# Debug logger for https://github.com/matrix-org/synapse/issues/4422
@@ -244,7 +247,7 @@ def __nonzero__(self) -> bool:
244247

245248

246249
class SyncHandler(object):
247-
def __init__(self, hs):
250+
def __init__(self, hs: "HomeServer"):
248251
self.hs_config = hs.config
249252
self.store = hs.get_datastore()
250253
self.notifier = hs.get_notifier()
@@ -717,9 +720,8 @@ async def compute_summary(
717720
]
718721

719722
missing_hero_state = await self.store.get_events(missing_hero_event_ids)
720-
missing_hero_state = missing_hero_state.values()
721723

722-
for s in missing_hero_state:
724+
for s in missing_hero_state.values():
723725
cache.set(s.state_key, s.event_id)
724726
state[(EventTypes.Member, s.state_key)] = s
725727

@@ -1771,7 +1773,7 @@ async def _generate_room_entry(
17711773
ignored_users: Set[str],
17721774
room_builder: "RoomSyncResultBuilder",
17731775
ephemeral: List[JsonDict],
1774-
tags: Optional[List[JsonDict]],
1776+
tags: Optional[Dict[str, Dict[str, Any]]],
17751777
account_data: Dict[str, JsonDict],
17761778
always_include: bool = False,
17771779
):

0 commit comments

Comments
 (0)