Skip to content

Commit 4b8824c

Browse files
Strict type checking fixes (#670)
Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 25f77c4 commit 4b8824c

34 files changed

Lines changed: 241 additions & 207 deletions

‎livekit-api/livekit/api/access_token.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ def verify(self, token: str, *, verify_signature: bool = True) -> Claims:
281281
return grant_claims
282282

283283

284-
def camel_to_snake(t: str):
284+
def camel_to_snake(t: str) -> str:
285285
return re.sub(r"(?<!^)(?=[A-Z])", "_", t).lower()
286286

287287

288-
def snake_to_lower_camel(t: str):
288+
def snake_to_lower_camel(t: str) -> str:
289289
return "".join(word.capitalize() if i else word for i, word in enumerate(t.split("_")))

‎livekit-api/livekit/api/livekit_api.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .sip_service import SipService
77
from .agent_dispatch_service import AgentDispatchService
88
from .connector_service import ConnectorService
9-
from typing import Optional
9+
from typing import Any, Optional
1010

1111

1212
class LiveKitAPI:
@@ -96,21 +96,21 @@ def connector(self) -> ConnectorService:
9696
"""Instance of the ConnectorService"""
9797
return self._connector
9898

99-
async def aclose(self):
99+
async def aclose(self) -> None:
100100
"""Close the API client
101101
102102
Call this before your application exits or when the API client is no longer needed."""
103103
# we do not close custom sessions, that's up to the caller
104-
if not self._custom_session:
104+
if not self._custom_session and self._session is not None:
105105
await self._session.close()
106106

107-
async def __aenter__(self):
107+
async def __aenter__(self) -> "LiveKitAPI":
108108
"""@private
109109
110110
Support for `async with`"""
111111
return self
112112

113-
async def __aexit__(self, exc_type, exc_val, exc_tb):
113+
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
114114
"""@private
115115
116116
Support for `async with`"""

‎livekit-rtc/livekit/rtc/_ffi_client.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
atexit.register(_resource_files.close)
3535

3636

37-
def _lib_name():
37+
def _lib_name() -> Optional[str]:
3838
if platform.system() == "Linux":
3939
return "liblivekit_ffi.so"
4040
elif platform.system() == "Darwin":
@@ -44,7 +44,7 @@ def _lib_name():
4444
return None
4545

4646

47-
def get_ffi_lib():
47+
def get_ffi_lib() -> ctypes.CDLL:
4848
# allow to override the lib path using an env var
4949
libpath = os.environ.get("LIVEKIT_LIB_PATH", "").strip()
5050
if libpath:
@@ -73,7 +73,7 @@ def __init__(self, handle: int) -> None:
7373
self.handle = handle
7474
self._disposed = False
7575

76-
def __del__(self):
76+
def __del__(self) -> None:
7777
self.dispose()
7878

7979
@property
@@ -149,7 +149,7 @@ def unsubscribe(self, queue: Queue[T]) -> None:
149149
break
150150

151151

152-
@ctypes.CFUNCTYPE(None, ctypes.POINTER(ctypes.c_uint8), ctypes.c_size_t)
152+
@ctypes.CFUNCTYPE(None, ctypes.POINTER(ctypes.c_uint8), ctypes.c_size_t) # type: ignore[untyped-decorator]
153153
def ffi_event_callback(
154154
data_ptr: ctypes.POINTER(ctypes.c_uint8), # type: ignore
155155
data_len: ctypes.c_size_t,
@@ -255,7 +255,7 @@ def __init__(self) -> None:
255255
ffi_lib = self._ffi_lib
256256

257257
@atexit.register
258-
def _dispose_lk_ffi():
258+
def _dispose_lk_ffi() -> None:
259259
ffi_lib.livekit_ffi_dispose()
260260

261261
@property

‎livekit-rtc/livekit/rtc/_utils.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
from collections import deque
1818
import ctypes
1919
import random
20-
from typing import Callable, Generator, Generic, List, TypeVar, Union
20+
from typing import Any, Callable, Generator, Generic, List, TypeVar, Union
2121

2222
logger = logging.getLogger("livekit")
2323

2424

2525
class classproperty(object):
26-
def __init__(self, f):
27-
self.f = classmethod(f)
26+
def __init__(self, f: Callable[..., Any]) -> None:
27+
self.f: Any = classmethod(f)
2828

29-
def __get__(self, *a):
29+
def __get__(self, *a: Any) -> Any:
3030
return self.f.__get__(*a)()
3131

3232

@@ -146,7 +146,7 @@ async def join(self) -> None:
146146
_base62_characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
147147

148148

149-
def generate_random_base62(length=12):
149+
def generate_random_base62(length: int = 12) -> str:
150150
"""
151151
Generate a random base62 encoded string of a specified length.
152152

‎livekit-rtc/livekit/rtc/audio_frame.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def __repr__(self) -> str:
193193
)
194194

195195
@classmethod
196-
def __get_pydantic_core_schema__(cls, *_: Any):
196+
def __get_pydantic_core_schema__(cls, *_: Any) -> Any:
197197
from pydantic_core import core_schema
198198
import base64
199199

‎livekit-rtc/livekit/rtc/audio_stream.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(
6565
num_channels: int = 1,
6666
frame_size_ms: int | None = None,
6767
noise_cancellation: Optional[NoiseCancellationOptions | FrameProcessor[AudioFrame]] = None,
68-
**kwargs,
68+
**kwargs: Any,
6969
) -> None:
7070
"""Initialize an `AudioStream` instance.
7171
@@ -273,7 +273,7 @@ def _create_owned_stream_from_participant(
273273
resp = FfiClient.instance.request(req)
274274
return resp.audio_stream_from_participant.stream
275275

276-
async def _run(self):
276+
async def _run(self) -> None:
277277
while True:
278278
event = await self._ffi_queue.wait_for(self._is_event)
279279
audio_event: proto_audio_frame.AudioStreamEvent = event.audio_stream_event

‎livekit-rtc/livekit/rtc/data_stream.py‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ def __init__(
6666
)
6767
self._queue: asyncio.Queue[proto_DataStream.Chunk | None] = asyncio.Queue()
6868

69-
async def _on_chunk_update(self, chunk: proto_DataStream.Chunk):
69+
async def _on_chunk_update(self, chunk: proto_DataStream.Chunk) -> None:
7070
await self._queue.put(chunk)
7171

72-
async def _on_stream_close(self, trailer: proto_DataStream.Trailer):
72+
async def _on_stream_close(self, trailer: proto_DataStream.Trailer) -> None:
7373
self.info.attributes = self.info.attributes or {}
7474
self.info.attributes.update(trailer.attributes)
7575
await self._queue.put(None)
@@ -114,10 +114,10 @@ def __init__(self, header: proto_DataStream.Header, capacity: int = 0) -> None:
114114
)
115115
self._queue: asyncio.Queue[proto_DataStream.Chunk | None] = asyncio.Queue(capacity)
116116

117-
async def _on_chunk_update(self, chunk: proto_DataStream.Chunk):
117+
async def _on_chunk_update(self, chunk: proto_DataStream.Chunk) -> None:
118118
await self._queue.put(chunk)
119119

120-
async def _on_stream_close(self, trailer: proto_DataStream.Trailer):
120+
async def _on_stream_close(self, trailer: proto_DataStream.Trailer) -> None:
121121
self.info.attributes = self.info.attributes or {}
122122
self.info.attributes.update(trailer.attributes)
123123
await self._queue.put(None)
@@ -166,7 +166,7 @@ def __init__(
166166
self._sender_identity = sender_identity or self._local_participant.identity
167167
self._closed = False
168168

169-
async def _send_header(self):
169+
async def _send_header(self) -> None:
170170
req = proto_ffi.FfiRequest(
171171
send_stream_header=proto_room.SendStreamHeaderRequest(
172172
header=self._header,
@@ -188,7 +188,7 @@ async def _send_header(self):
188188
if cb.send_stream_header.error:
189189
raise ConnectionError(cb.send_stream_header.error)
190190

191-
async def _send_chunk(self, chunk: proto_DataStream.Chunk):
191+
async def _send_chunk(self, chunk: proto_DataStream.Chunk) -> None:
192192
if self._closed:
193193
raise RuntimeError(f"Cannot send chunk after stream is closed: {chunk}")
194194
req = proto_ffi.FfiRequest(
@@ -212,7 +212,7 @@ async def _send_chunk(self, chunk: proto_DataStream.Chunk):
212212
if cb.send_stream_chunk.error:
213213
raise ConnectionError(cb.send_stream_chunk.error)
214214

215-
async def _send_trailer(self, trailer: proto_DataStream.Trailer):
215+
async def _send_trailer(self, trailer: proto_DataStream.Trailer) -> None:
216216
req = proto_ffi.FfiRequest(
217217
send_stream_trailer=proto_room.SendStreamTrailerRequest(
218218
trailer=trailer,
@@ -230,10 +230,12 @@ async def _send_trailer(self, trailer: proto_DataStream.Trailer):
230230
finally:
231231
FfiClient.instance.queue.unsubscribe(queue)
232232

233-
if cb.send_stream_chunk.error:
233+
if cb.send_stream_trailer.error:
234234
raise ConnectionError(cb.send_stream_trailer.error)
235235

236-
async def aclose(self, *, reason: str = "", attributes: Optional[Dict[str, str]] = None):
236+
async def aclose(
237+
self, *, reason: str = "", attributes: Optional[Dict[str, str]] = None
238+
) -> None:
237239
if self._closed:
238240
raise RuntimeError("Stream already closed")
239241
self._closed = True
@@ -281,7 +283,7 @@ def __init__(
281283
)
282284
self._write_lock = asyncio.Lock()
283285

284-
async def write(self, text: str):
286+
async def write(self, text: str) -> None:
285287
async with self._write_lock:
286288
for chunk in split_utf8(text, STREAM_CHUNK_SIZE):
287289
content = chunk
@@ -333,7 +335,7 @@ def __init__(
333335
)
334336
self._write_lock = asyncio.Lock()
335337

336-
async def write(self, data: bytes):
338+
async def write(self, data: bytes) -> None:
337339
async with self._write_lock:
338340
chunked_data = [
339341
data[i : i + STREAM_CHUNK_SIZE] for i in range(0, len(data), STREAM_CHUNK_SIZE)

‎livekit-rtc/livekit/rtc/data_track.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def is_published(self) -> bool:
117117
req.local_data_track_is_published.track_handle = self._ffi_handle.handle
118118

119119
resp = FfiClient.instance.request(req)
120-
return resp.local_data_track_is_published.is_published
120+
return bool(resp.local_data_track_is_published.is_published)
121121

122122
async def unpublish(self) -> None:
123123
"""Unpublishes the track."""
@@ -181,7 +181,7 @@ def is_published(self) -> bool:
181181
req.remote_data_track_is_published.track_handle = self._ffi_handle.handle
182182

183183
resp = FfiClient.instance.request(req)
184-
return resp.remote_data_track_is_published.is_published
184+
return bool(resp.remote_data_track_is_published.is_published)
185185

186186
def __repr__(self) -> str:
187187
return (

‎livekit-rtc/livekit/rtc/e2ee.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
from dataclasses import dataclass, field
16-
from typing import List, Optional
16+
from typing import List, Optional, cast
1717

1818
from ._ffi_client import FfiClient
1919
from ._proto import e2ee_pb2 as proto_e2ee
@@ -89,7 +89,7 @@ def export_shared_key(self, key_index: int) -> bytes:
8989
req.e2ee.get_shared_key.key_index = key_index
9090
resp = FfiClient.instance.request(req)
9191
key = resp.e2ee.get_shared_key.key
92-
return key
92+
return cast(bytes, key)
9393

9494
def ratchet_shared_key(self, key_index: int) -> bytes:
9595
"""Ratchets the shared encryption key to a new key.
@@ -112,7 +112,7 @@ def ratchet_shared_key(self, key_index: int) -> bytes:
112112
resp = FfiClient.instance.request(req)
113113

114114
new_key = resp.e2ee.ratchet_shared_key.new_key
115-
return new_key
115+
return cast(bytes, new_key)
116116

117117
def set_key(self, participant_identity: str, key: bytes, key_index: int) -> None:
118118
"""Sets the encryption key for a specific participant.
@@ -157,7 +157,7 @@ def export_key(self, participant_identity: str, key_index: int) -> bytes:
157157
req.e2ee.get_key.key_index = key_index
158158
resp = FfiClient.instance.request(req)
159159
key = resp.e2ee.get_key.key
160-
return key
160+
return cast(bytes, key)
161161

162162
def ratchet_key(self, participant_identity: str, key_index: int) -> bytes:
163163
"""Ratchets the encryption key for a specific participant to a new key.
@@ -181,7 +181,7 @@ def ratchet_key(self, participant_identity: str, key_index: int) -> bytes:
181181

182182
resp = FfiClient.instance.request(req)
183183
new_key = resp.e2ee.ratchet_key.new_key
184-
return new_key
184+
return cast(bytes, new_key)
185185

186186

187187
class FrameCryptor:

‎livekit-rtc/livekit/rtc/event_emitter.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import inspect
22
import asyncio
3-
from typing import Callable, Dict, Set, Optional, Generic, TypeVar
3+
from typing import Any, Callable, Dict, Set, Optional, Generic, TypeVar
44

55
from .log import logger
66

@@ -14,7 +14,7 @@ def __init__(self) -> None:
1414
"""
1515
self._events: Dict[T_contra, Set[Callable]] = dict()
1616

17-
def emit(self, event: T_contra, *args) -> None:
17+
def emit(self, event: T_contra, *args: Any) -> None:
1818
"""
1919
Trigger all callbacks associated with the given event.
2020
@@ -104,7 +104,7 @@ def greet_once(name):
104104
"""
105105
if callback is not None:
106106

107-
def once_callback(*args, **kwargs):
107+
def once_callback(*args: Any, **kwargs: Any) -> None:
108108
self.off(event, once_callback)
109109
callback(*args, **kwargs)
110110

0 commit comments

Comments
 (0)