@@ -315,10 +315,11 @@ def __init__(
315315 redacts : DefaultDictProperty [Optional [str ]] = DefaultDictProperty ("redacts" , None )
316316 room_id : DictProperty [str ] = DictProperty ("room_id" )
317317 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?)
320320 # 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).
322323 state_key : DictProperty [str ] = DictProperty ("state_key" )
323324 type : DictProperty [str ] = DictProperty ("type" )
324325 user_id : DictProperty [str ] = DictProperty ("sender" )
@@ -332,7 +333,11 @@ def membership(self) -> str:
332333 return self .content ["membership" ]
333334
334335 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" )
336341
337342 def get_dict (self ) -> JsonDict :
338343 d = dict (self ._dict )
0 commit comments