1616import ctypes
1717import logging
1818from dataclasses import dataclass , field
19- from typing import Dict , Literal , Optional
19+ from typing import Dict , Literal , Optional , cast
2020
2121from ._event_emitter import EventEmitter
2222from ._ffi_client import FfiClient , FfiHandle
@@ -329,6 +329,7 @@ def _on_room_event(self, event: proto_room.RoomEvent):
329329 sid = event .track_muted .participant_sid
330330 # TODO: pass participant identity
331331 participant = self ._retrieve_participant (sid , "" )
332+ assert isinstance (participant , Participant )
332333 publication = participant .tracks [event .track_muted .track_sid ]
333334 publication ._info .muted = True
334335 if publication .track :
@@ -339,6 +340,7 @@ def _on_room_event(self, event: proto_room.RoomEvent):
339340 sid = event .track_unmuted .participant_sid
340341 # TODO: pass participant identity
341342 participant = self ._retrieve_participant (sid , "" )
343+ assert isinstance (participant , Participant )
342344 publication = participant .tracks [event .track_unmuted .track_sid ]
343345 publication ._info .muted = False
344346 if publication .track :
@@ -349,7 +351,9 @@ def _on_room_event(self, event: proto_room.RoomEvent):
349351 speakers : list [Participant ] = []
350352 # TODO: pass participant identity
351353 for sid in event .active_speakers_changed .participant_sids :
352- speakers .append (self ._retrieve_participant (sid , "" ))
354+ participant = self ._retrieve_participant (sid , "" )
355+ assert isinstance (participant , Participant )
356+ speakers .append (participant )
353357
354358 self .emit ("active_speakers_changed" , speakers )
355359 elif which == "room_metadata_changed" :
@@ -360,6 +364,7 @@ def _on_room_event(self, event: proto_room.RoomEvent):
360364 sid = event .participant_metadata_changed .participant_sid
361365 # TODO: pass participant identity
362366 participant = self ._retrieve_participant (sid , "" )
367+ assert isinstance (participant , Participant )
363368 old_metadata = participant .metadata
364369 participant ._info .metadata = event .participant_metadata_changed .metadata
365370 self .emit (
@@ -372,6 +377,7 @@ def _on_room_event(self, event: proto_room.RoomEvent):
372377 sid = event .participant_name_changed .participant_sid
373378 # TODO: pass participant identity
374379 participant = self ._retrieve_participant (sid , "" )
380+ assert isinstance (participant , Participant )
375381 old_name = participant .name
376382 participant ._info .name = event .participant_name_changed .name
377383 self .emit (
@@ -399,8 +405,11 @@ def _on_room_event(self, event: proto_room.RoomEvent):
399405
400406 data = bytes (native_data )
401407 FfiHandle (owned_buffer_info .handle .id )
402- rparticipant = self ._retrieve_remote_participant (
403- packet .participant_sid , packet .participant_identity
408+ rparticipant = cast (
409+ RemoteParticipant ,
410+ self ._retrieve_remote_participant (
411+ packet .participant_sid , packet .participant_identity
412+ ),
404413 )
405414 self .emit (
406415 "data_received" ,
@@ -412,8 +421,11 @@ def _on_room_event(self, event: proto_room.RoomEvent):
412421 ),
413422 )
414423 elif which_val == "sip_dtmf" :
415- rparticipant = self ._retrieve_remote_participant (
416- packet .participant_sid , packet .participant_identity
424+ rparticipant = cast (
425+ RemoteParticipant ,
426+ self ._retrieve_remote_participant (
427+ packet .participant_sid , packet .participant_identity
428+ ),
417429 )
418430 self .emit (
419431 "sip_dtmf_received" ,
@@ -446,16 +458,16 @@ def _on_room_event(self, event: proto_room.RoomEvent):
446458
447459 def _retrieve_remote_participant (
448460 self , sid : str , identity : str
449- ) -> RemoteParticipant :
461+ ) -> Optional [ RemoteParticipant ] :
450462 """Retrieve a remote participant by sid or identity"""
451463 participant = None
452464 if identity :
453465 participant = self .participants_by_identity [identity ]
454- if not participant :
466+ if not participant and sid in self . participants :
455467 participant = self .participants [sid ]
456468 return participant
457469
458- def _retrieve_participant (self , sid : str , identity : str ) -> Participant :
470+ def _retrieve_participant (self , sid : str , identity : str ) -> Optional [ Participant ] :
459471 """Retrieve a participant by sid or identity,
460472 returns the LocalParticipant if sid or identity matches"""
461473 if identity and identity == self .local_participant .identity :
0 commit comments