Skip to content

Commit c1111ac

Browse files
authored
fix typing of track_publications using covariant Mapping (#287)
1 parent 135edaa commit c1111ac

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

livekit-rtc/livekit/rtc/participant.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from __future__ import annotations
1616

1717
import ctypes
18-
from typing import List, Union
18+
from typing import List, Mapping, Union
19+
from abc import abstractmethod, ABC
1920

2021
from ._ffi_client import FfiClient, FfiHandle
2122
from ._proto import ffi_pb2 as proto_ffi
@@ -61,18 +62,18 @@ def __init__(self, message: str) -> None:
6162
self.message = message
6263

6364

64-
class Participant:
65+
class Participant(ABC):
6566
def __init__(self, owned_info: proto_participant.OwnedParticipant) -> None:
6667
self._info = owned_info.info
6768
self._ffi_handle = FfiHandle(owned_info.handle.id)
68-
self._track_publications: dict[str, TrackPublication] = {}
6969

7070
@property
71-
def track_publications(self) -> dict[str, TrackPublication]:
71+
@abstractmethod
72+
def track_publications(self) -> Mapping[str, TrackPublication]:
7273
"""
7374
A dictionary of track publications associated with the participant.
7475
"""
75-
return self._track_publications
76+
...
7677

7778
@property
7879
def sid(self) -> str:
@@ -111,7 +112,14 @@ def __init__(
111112
) -> None:
112113
super().__init__(owned_info)
113114
self._room_queue = room_queue
114-
self.track_publications: dict[str, LocalTrackPublication] = {} # type: ignore
115+
self._track_publications: dict[str, LocalTrackPublication] = {} # type: ignore
116+
117+
@property
118+
def track_publications(self) -> Mapping[str, LocalTrackPublication]:
119+
"""
120+
A dictionary of track publications associated with the participant.
121+
"""
122+
return self._track_publications
115123

116124
async def publish_data(
117125
self,
@@ -330,7 +338,7 @@ async def publish_track(
330338
track_publication = LocalTrackPublication(cb.publish_track.publication)
331339
track_publication.track = track
332340
track._info.sid = track_publication.sid
333-
self.track_publications[track_publication.sid] = track_publication
341+
self._track_publications[track_publication.sid] = track_publication
334342

335343
queue.task_done()
336344
return track_publication
@@ -361,7 +369,7 @@ async def unpublish_track(self, track_sid: str) -> None:
361369
if cb.unpublish_track.error:
362370
raise UnpublishTrackError(cb.unpublish_track.error)
363371

364-
publication = self.track_publications.pop(track_sid)
372+
publication = self._track_publications.pop(track_sid)
365373
publication.track = None
366374
queue.task_done()
367375
finally:
@@ -374,7 +382,14 @@ def __repr__(self) -> str:
374382
class RemoteParticipant(Participant):
375383
def __init__(self, owned_info: proto_participant.OwnedParticipant) -> None:
376384
super().__init__(owned_info)
377-
self.track_publications: dict[str, RemoteTrackPublication] = {} # type: ignore
385+
self._track_publications: dict[str, RemoteTrackPublication] = {} # type: ignore
386+
387+
@property
388+
def track_publications(self) -> Mapping[str, RemoteTrackPublication]:
389+
"""
390+
A dictionary of track publications associated with the participant.
391+
"""
392+
return self._track_publications
378393

379394
def __repr__(self) -> str:
380395
return f"rtc.RemoteParticipant(sid={self.sid}, identity={self.identity}, name={self.name})"

livekit-rtc/livekit/rtc/room.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import ctypes
1818
import logging
1919
from dataclasses import dataclass, field
20-
from typing import Callable, Dict, Literal, Optional, cast
20+
from typing import Callable, Dict, Literal, Optional, cast, Mapping
2121

2222
from .event_emitter import EventEmitter
2323
from ._ffi_client import FfiClient, FfiHandle
@@ -174,7 +174,7 @@ def connection_state(self) -> ConnectionState.ValueType:
174174
return self._connection_state
175175

176176
@property
177-
def remote_participants(self) -> dict[str, RemoteParticipant]:
177+
def remote_participants(self) -> Mapping[str, RemoteParticipant]:
178178
"""Gets the remote participants in the room.
179179
180180
Returns:
@@ -389,7 +389,7 @@ def on_participant_connected(participant):
389389
# add the initial remote participant tracks
390390
for owned_publication_info in pt.publications:
391391
publication = RemoteTrackPublication(owned_publication_info)
392-
rp.track_publications[publication.sid] = publication
392+
rp._track_publications[publication.sid] = publication
393393

394394
# start listening to room events
395395
self._task = self._loop.create_task(self._listen_task())
@@ -466,13 +466,13 @@ def _on_room_event(self, event: proto_room.RoomEvent):
466466
event.track_published.participant_identity
467467
]
468468
rpublication = RemoteTrackPublication(event.track_published.publication)
469-
rparticipant.track_publications[rpublication.sid] = rpublication
469+
rparticipant._track_publications[rpublication.sid] = rpublication
470470
self.emit("track_published", rpublication, rparticipant)
471471
elif which == "track_unpublished":
472472
rparticipant = self._remote_participants[
473473
event.track_unpublished.participant_identity
474474
]
475-
rpublication = rparticipant.track_publications.pop(
475+
rpublication = rparticipant._track_publications.pop(
476476
event.track_unpublished.publication_sid
477477
)
478478
self.emit("track_unpublished", rpublication, rparticipant)

0 commit comments

Comments
 (0)