@@ -315,10 +315,11 @@ def __init__(
315
315
redacts : DefaultDictProperty [Optional [str ]] = DefaultDictProperty ("redacts" , None )
316
316
room_id : DictProperty [str ] = DictProperty ("room_id" )
317
317
sender : DictProperty [str ] = DictProperty ("sender" )
318
- # TODO state_key should be Optional[str], this is generally asserted in Synapse
319
- # by calling is_state() first (which ensures this ), but it is hard (not possible?)
318
+ # TODO state_key should be Optional[str]. This is generally asserted in Synapse
319
+ # by calling is_state() first (which ensures it is not None ), but it is hard (not possible?)
320
320
# to properly annotate that calling is_state() asserts that state_key exists
321
- # and is non-None.
321
+ # and is non-None. It would be better to replace such direct references with
322
+ # get_state_key() (and a check for None).
322
323
state_key : DictProperty [str ] = DictProperty ("state_key" )
323
324
type : DictProperty [str ] = DictProperty ("type" )
324
325
user_id : DictProperty [str ] = DictProperty ("sender" )
@@ -332,7 +333,11 @@ def membership(self) -> str:
332
333
return self .content ["membership" ]
333
334
334
335
def is_state (self ) -> bool :
335
- return hasattr (self , "state_key" ) and self .state_key is not None
336
+ return self .get_state_key () is not None
337
+
338
+ def get_state_key (self ) -> Optional [str ]:
339
+ """Get the state key of this event, or None if it's not a state event"""
340
+ return self ._dict .get ("state_key" )
336
341
337
342
def get_dict (self ) -> JsonDict :
338
343
d = dict (self ._dict )
0 commit comments