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

Commit 20bfb53

Browse files
committed
Merge commit '5dd73d029' into anoa/dinsic_release_1_21_x
* commit '5dd73d029': Add type hints to handlers.message and events.builder (#8067)
2 parents 33d971b + 5dd73d0 commit 20bfb53

File tree

7 files changed

+61
-41
lines changed

7 files changed

+61
-41
lines changed

changelog.d/8067.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add type hints to `synapse.handlers.message` and `synapse.events.builder`.

mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,6 @@ ignore_missing_imports = True
8181

8282
[mypy-rust_python_jaeger_reporter.*]
8383
ignore_missing_imports = True
84+
85+
[mypy-nacl.*]
86+
ignore_missing_imports = True

synapse/events/builder.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import attr
1818
from nacl.signing import SigningKey
1919

20+
from synapse.api.auth import Auth
2021
from synapse.api.constants import MAX_DEPTH
2122
from synapse.api.errors import UnsupportedRoomVersionError
2223
from synapse.api.room_versions import (
@@ -27,6 +28,8 @@
2728
)
2829
from synapse.crypto.event_signing import add_hashes_and_signatures
2930
from synapse.events import EventBase, _EventInternalMetadata, make_event_from_dict
31+
from synapse.state import StateHandler
32+
from synapse.storage.databases.main import DataStore
3033
from synapse.types import EventID, JsonDict
3134
from synapse.util import Clock
3235
from synapse.util.stringutils import random_string
@@ -42,45 +45,46 @@ class EventBuilder(object):
4245
4346
Attributes:
4447
room_version: Version of the target room
45-
room_id (str)
46-
type (str)
47-
sender (str)
48-
content (dict)
49-
unsigned (dict)
50-
internal_metadata (_EventInternalMetadata)
51-
52-
_state (StateHandler)
53-
_auth (synapse.api.Auth)
54-
_store (DataStore)
55-
_clock (Clock)
56-
_hostname (str): The hostname of the server creating the event
48+
room_id
49+
type
50+
sender
51+
content
52+
unsigned
53+
internal_metadata
54+
55+
_state
56+
_auth
57+
_store
58+
_clock
59+
_hostname: The hostname of the server creating the event
5760
_signing_key: The signing key to use to sign the event as the server
5861
"""
5962

60-
_state = attr.ib()
61-
_auth = attr.ib()
62-
_store = attr.ib()
63-
_clock = attr.ib()
64-
_hostname = attr.ib()
65-
_signing_key = attr.ib()
63+
_state = attr.ib(type=StateHandler)
64+
_auth = attr.ib(type=Auth)
65+
_store = attr.ib(type=DataStore)
66+
_clock = attr.ib(type=Clock)
67+
_hostname = attr.ib(type=str)
68+
_signing_key = attr.ib(type=SigningKey)
6669

6770
room_version = attr.ib(type=RoomVersion)
6871

69-
room_id = attr.ib()
70-
type = attr.ib()
71-
sender = attr.ib()
72+
room_id = attr.ib(type=str)
73+
type = attr.ib(type=str)
74+
sender = attr.ib(type=str)
7275

73-
content = attr.ib(default=attr.Factory(dict))
74-
unsigned = attr.ib(default=attr.Factory(dict))
76+
content = attr.ib(default=attr.Factory(dict), type=JsonDict)
77+
unsigned = attr.ib(default=attr.Factory(dict), type=JsonDict)
7578

7679
# These only exist on a subset of events, so they raise AttributeError if
7780
# someone tries to get them when they don't exist.
78-
_state_key = attr.ib(default=None)
79-
_redacts = attr.ib(default=None)
80-
_origin_server_ts = attr.ib(default=None)
81+
_state_key = attr.ib(default=None, type=Optional[str])
82+
_redacts = attr.ib(default=None, type=Optional[str])
83+
_origin_server_ts = attr.ib(default=None, type=Optional[int])
8184

8285
internal_metadata = attr.ib(
83-
default=attr.Factory(lambda: _EventInternalMetadata({}))
86+
default=attr.Factory(lambda: _EventInternalMetadata({})),
87+
type=_EventInternalMetadata,
8488
)
8589

8690
@property

synapse/handlers/message.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717
import logging
18-
from typing import TYPE_CHECKING, List, Optional, Tuple
18+
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple
1919

2020
from canonicaljson import encode_canonical_json, json
2121

@@ -94,11 +94,11 @@ def __init__(self, hs):
9494

9595
async def get_room_data(
9696
self,
97-
user_id: str = None,
98-
room_id: str = None,
99-
event_type: Optional[str] = None,
100-
state_key: str = "",
101-
is_guest: bool = False,
97+
user_id: str,
98+
room_id: str,
99+
event_type: str,
100+
state_key: str,
101+
is_guest: bool,
102102
) -> dict:
103103
""" Get data from a room.
104104
@@ -410,7 +410,7 @@ def __init__(self, hs: "HomeServer"):
410410
#
411411
# map from room id to time-of-last-attempt.
412412
#
413-
self._rooms_to_exclude_from_dummy_event_insertion = {} # type: dict[str, int]
413+
self._rooms_to_exclude_from_dummy_event_insertion = {} # type: Dict[str, int]
414414

415415
# we need to construct a ConsentURIBuilder here, as it checks that the necessary
416416
# config options, but *only* if we have a configuration for which we are
@@ -710,7 +710,7 @@ async def deduplicate_state_event(
710710
async def create_and_send_nonmember_event(
711711
self,
712712
requester: Requester,
713-
event_dict: EventBase,
713+
event_dict: dict,
714714
ratelimit: bool = True,
715715
txn_id: Optional[str] = None,
716716
) -> Tuple[EventBase, int]:
@@ -974,7 +974,7 @@ async def persist_and_notify_client_event(
974974
# Validate a newly added alias or newly added alt_aliases.
975975

976976
original_alias = None
977-
original_alt_aliases = set()
977+
original_alt_aliases = [] # type: List[str]
978978

979979
original_event_id = event.unsigned.get("replaces_state")
980980
if original_event_id:
@@ -1022,6 +1022,10 @@ def is_inviter_member_event(e):
10221022

10231023
current_state_ids = await context.get_current_state_ids()
10241024

1025+
# We know this event is not an outlier, so this must be
1026+
# non-None.
1027+
assert current_state_ids is not None
1028+
10251029
state_to_include_ids = [
10261030
e_id
10271031
for k, e_id in current_state_ids.items()

synapse/handlers/room_member.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import abc
1717
import logging
1818
from http import HTTPStatus
19-
from typing import Dict, Iterable, List, Optional, Tuple, Union
19+
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Tuple, Union
2020

2121
from unpaddedbase64 import encode_base64
2222

@@ -37,6 +37,10 @@
3737

3838
from ._base import BaseHandler
3939

40+
if TYPE_CHECKING:
41+
from synapse.server import HomeServer
42+
43+
4044
logger = logging.getLogger(__name__)
4145

4246

@@ -48,7 +52,7 @@ class RoomMemberHandler(object):
4852

4953
__metaclass__ = abc.ABCMeta
5054

51-
def __init__(self, hs):
55+
def __init__(self, hs: "HomeServer"):
5256
self.hs = hs
5357
self.store = hs.get_datastore()
5458
self.auth = hs.get_auth()
@@ -208,7 +212,7 @@ async def _local_membership_update(
208212
return duplicate.event_id, stream_id
209213

210214
stream_id = await self.event_creation_handler.handle_new_client_event(
211-
requester, event, context, extra_users=[target], ratelimit=ratelimit
215+
requester, event, context, extra_users=[target], ratelimit=ratelimit,
212216
)
213217

214218
prev_state_ids = await context.get_prev_state_ids()
@@ -1055,7 +1059,7 @@ async def _remote_join(
10551059

10561060
check_complexity = self.hs.config.limit_remote_rooms.enabled
10571061
if check_complexity and self.hs.config.limit_remote_rooms.admins_can_join:
1058-
check_complexity = not await self.hs.auth.is_server_admin(user)
1062+
check_complexity = not await self.auth.is_server_admin(user)
10591063

10601064
if check_complexity:
10611065
# Fetch the room complexity

tests/rest/client/test_retention.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ def _test_retention_event_purged(self, room_id, increment):
145145
# Get the create event to, later, check that we can still access it.
146146
message_handler = self.hs.get_message_handler()
147147
create_event = self.get_success(
148-
message_handler.get_room_data(self.user_id, room_id, EventTypes.Create)
148+
message_handler.get_room_data(
149+
self.user_id, room_id, EventTypes.Create, state_key="", is_guest=False
150+
)
149151
)
150152

151153
# Send a first event to the room. This is the event we'll want to be purged at the

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,15 @@ commands = mypy \
180180
synapse/appservice \
181181
synapse/config \
182182
synapse/event_auth.py \
183+
synapse/events/builder.py \
183184
synapse/events/spamcheck.py \
184185
synapse/federation \
185186
synapse/handlers/auth.py \
186187
synapse/handlers/cas_handler.py \
187188
synapse/handlers/directory.py \
188189
synapse/handlers/federation.py \
189190
synapse/handlers/identity.py \
191+
synapse/handlers/message.py \
190192
synapse/handlers/oidc_handler.py \
191193
synapse/handlers/presence.py \
192194
synapse/handlers/room_member.py \

0 commit comments

Comments
 (0)