1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414import logging
15- from typing import Any , Dict , List , Optional , Tuple , Union
15+ from typing import TYPE_CHECKING , Any , Dict , List , Optional , Tuple , Union
1616
1717import attr
1818from nacl .signing import SigningKey
1919
20- from synapse .api .auth import Auth
2120from synapse .api .constants import MAX_DEPTH
2221from synapse .api .errors import UnsupportedRoomVersionError
2322from synapse .api .room_versions import (
3433from synapse .util import Clock
3534from synapse .util .stringutils import random_string
3635
36+ if TYPE_CHECKING :
37+ from synapse .api .auth import Auth
38+ from synapse .server import HomeServer
39+
3740logger = logging .getLogger (__name__ )
3841
3942
40- @attr .s (slots = True , cmp = False , frozen = True )
43+ @attr .s (slots = True , cmp = False , frozen = True , auto_attribs = True )
4144class EventBuilder :
4245 """A format independent event builder used to build up the event content
4346 before signing the event.
@@ -62,31 +65,30 @@ class EventBuilder:
6265 _signing_key: The signing key to use to sign the event as the server
6366 """
6467
65- _state = attr . ib ( type = StateHandler )
66- _auth = attr . ib ( type = Auth )
67- _store = attr . ib ( type = DataStore )
68- _clock = attr . ib ( type = Clock )
69- _hostname = attr . ib ( type = str )
70- _signing_key = attr . ib ( type = SigningKey )
68+ _state : StateHandler
69+ _auth : " Auth"
70+ _store : DataStore
71+ _clock : Clock
72+ _hostname : str
73+ _signing_key : SigningKey
7174
72- room_version = attr . ib ( type = RoomVersion )
75+ room_version : RoomVersion
7376
74- room_id = attr . ib ( type = str )
75- type = attr . ib ( type = str )
76- sender = attr . ib ( type = str )
77+ room_id : str
78+ type : str
79+ sender : str
7780
78- content = attr .ib ( default = attr . Factory (dict ), type = JsonDict )
79- unsigned = attr .ib ( default = attr . Factory (dict ), type = JsonDict )
81+ content : JsonDict = attr .Factory (dict )
82+ unsigned : JsonDict = attr .Factory (dict )
8083
8184 # These only exist on a subset of events, so they raise AttributeError if
8285 # someone tries to get them when they don't exist.
83- _state_key = attr . ib ( default = None , type = Optional [str ])
84- _redacts = attr . ib ( default = None , type = Optional [str ])
85- _origin_server_ts = attr . ib ( default = None , type = Optional [int ])
86+ _state_key : Optional [str ] = None
87+ _redacts : Optional [str ] = None
88+ _origin_server_ts : Optional [int ] = None
8689
87- internal_metadata = attr .ib (
88- default = attr .Factory (lambda : _EventInternalMetadata ({})),
89- type = _EventInternalMetadata ,
90+ internal_metadata : _EventInternalMetadata = attr .Factory (
91+ lambda : _EventInternalMetadata ({})
9092 )
9193
9294 @property
@@ -184,7 +186,7 @@ async def build(
184186
185187
186188class EventBuilderFactory :
187- def __init__ (self , hs ):
189+ def __init__ (self , hs : "HomeServer" ):
188190 self .clock = hs .get_clock ()
189191 self .hostname = hs .hostname
190192 self .signing_key = hs .signing_key
@@ -193,15 +195,14 @@ def __init__(self, hs):
193195 self .state = hs .get_state_handler ()
194196 self .auth = hs .get_auth ()
195197
196- def new (self , room_version , key_values ) :
198+ def new (self , room_version : str , key_values : dict ) -> EventBuilder :
197199 """Generate an event builder appropriate for the given room version
198200
199201 Deprecated: use for_room_version with a RoomVersion object instead
200202
201203 Args:
202- room_version (str): Version of the room that we're creating an event builder
203- for
204- key_values (dict): Fields used as the basis of the new event
204+ room_version: Version of the room that we're creating an event builder for
205+ key_values: Fields used as the basis of the new event
205206
206207 Returns:
207208 EventBuilder
@@ -212,13 +213,15 @@ def new(self, room_version, key_values):
212213 raise UnsupportedRoomVersionError ()
213214 return self .for_room_version (v , key_values )
214215
215- def for_room_version (self , room_version , key_values ):
216+ def for_room_version (
217+ self , room_version : RoomVersion , key_values : dict
218+ ) -> EventBuilder :
216219 """Generate an event builder appropriate for the given room version
217220
218221 Args:
219- room_version (synapse.api.room_versions.RoomVersion) :
222+ room_version:
220223 Version of the room that we're creating an event builder for
221- key_values (dict) : Fields used as the basis of the new event
224+ key_values: Fields used as the basis of the new event
222225
223226 Returns:
224227 EventBuilder
@@ -286,15 +289,15 @@ def create_local_event_from_event_dict(
286289_event_id_counter = 0
287290
288291
289- def _create_event_id (clock , hostname ) :
292+ def _create_event_id (clock : Clock , hostname : str ) -> str :
290293 """Create a new event ID
291294
292295 Args:
293- clock (Clock)
294- hostname (str) : The server name for the event ID
296+ clock
297+ hostname: The server name for the event ID
295298
296299 Returns:
297- str
300+ The new event ID
298301 """
299302
300303 global _event_id_counter
0 commit comments