Skip to content

Commit 5101d70

Browse files
committed
Fix linting errors
Signed-off-by: Jared O'Connell <[email protected]>
1 parent c84d160 commit 5101d70

File tree

5 files changed

+98
-44
lines changed

5 files changed

+98
-44
lines changed

src/guidellm/utils/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
"EndlessTextCreator",
8282
"InfoMixin",
8383
"IntegerRangeSampler",
84-
"camelize_str",
8584
"InterProcessMessaging",
8685
"InterProcessMessagingManagerQueue",
8786
"InterProcessMessagingPipe",
@@ -107,14 +106,15 @@
107106
"ThreadSafeSingletonMixin",
108107
"TimeRunningStats",
109108
"all_defined",
109+
"camelize_str",
110110
"check_load_processor",
111111
"clean_text",
112112
"filter_text",
113113
"format_value_display",
114114
"get_literal_vals",
115115
"is_punctuation",
116-
"recursive_key_update",
117116
"load_text",
117+
"recursive_key_update",
118118
"safe_add",
119119
"safe_divide",
120120
"safe_format_timestamp",

src/guidellm/utils/encoding.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
import json
1414
from collections.abc import Mapping
15-
from typing import Annotated, Any, cast, ClassVar, Generic, Literal, Optional, TypeVar
15+
from typing import Annotated, Any, ClassVar, Generic, Literal, Optional, TypeVar, cast
1616

1717
try:
18-
import msgpack # type: ignore[import-untyped] # Optional dependency
18+
import msgpack # type: ignore[import-untyped] # Optional dependency
1919
from msgpack import Packer, Unpacker
2020

2121
HAS_MSGPACK = True
@@ -24,16 +24,20 @@
2424
HAS_MSGPACK = False
2525

2626
try:
27-
from msgspec.msgpack import Decoder as MsgspecDecoder # type: ignore[import-not-found] # Optional dependency
28-
from msgspec.msgpack import Encoder as MsgspecEncoder # type: ignore[import-not-found] # Optional dependency
27+
from msgspec.msgpack import ( # type: ignore[import-not-found] # Optional dependency
28+
Decoder as MsgspecDecoder,
29+
)
30+
from msgspec.msgpack import ( # type: ignore[import-not-found] # Optional dependency
31+
Encoder as MsgspecEncoder,
32+
)
2933

3034
HAS_MSGSPEC = True
3135
except ImportError:
3236
MsgspecDecoder = MsgspecEncoder = None
3337
HAS_MSGSPEC = False
3438

3539
try:
36-
import orjson # type: ignore[import-not-found] # Optional dependency
40+
import orjson # type: ignore[import-not-found] # Optional dependency
3741

3842
HAS_ORJSON = True
3943
except ImportError:
@@ -116,7 +120,7 @@ def encode_message(
116120
"""
117121
serialized = serializer.serialize(obj) if serializer else obj
118122

119-
return cast(MsgT, encoder.encode(serialized) if encoder else serialized)
123+
return cast("MsgT", encoder.encode(serialized) if encoder else serialized)
120124

121125
@classmethod
122126
def decode_message(
@@ -137,7 +141,9 @@ def decode_message(
137141
"""
138142
serialized = encoder.decode(message) if encoder else message
139143

140-
return cast(ObjT, serializer.deserialize(serialized) if serializer else serialized)
144+
return cast(
145+
"ObjT", serializer.deserialize(serialized) if serializer else serialized
146+
)
141147

142148
def __init__(
143149
self,
@@ -296,7 +302,14 @@ def _get_available_encoder_decoder(
296302
return None, None, None
297303

298304

299-
PayloadType = Literal['pydantic', 'python', 'collection_tuple', 'collection_sequence', 'collection_mapping']
305+
PayloadType = Literal[
306+
"pydantic",
307+
"python",
308+
"collection_tuple",
309+
"collection_sequence",
310+
"collection_mapping",
311+
]
312+
300313

301314
class Serializer:
302315
"""
@@ -518,7 +531,9 @@ def to_sequence(self, obj: Any) -> str | Any:
518531
payload_type = "python"
519532
payload = self.to_sequence_python(obj)
520533

521-
return self.pack_next_sequence(payload_type, payload if payload is not None else "", None)
534+
return self.pack_next_sequence(
535+
payload_type, payload if payload is not None else "", None
536+
)
522537

523538
def from_sequence(self, data: str | Any) -> Any: # noqa: C901, PLR0912
524539
"""
@@ -709,8 +724,10 @@ def pack_next_sequence( # noqa: C901, PLR0912
709724
delimiter = "|"
710725

711726
# Type ignores because types are enforced at runtime
712-
next_sequence = payload_type + delimiter + payload_len_output + delimiter + payload # type: ignore[operator]
713-
return current + next_sequence if current else next_sequence # type: ignore[operator]
727+
next_sequence = (
728+
payload_type + delimiter + payload_len_output + delimiter + payload # type: ignore[operator]
729+
)
730+
return current + next_sequence if current else next_sequence # type: ignore[operator]
714731

715732
def unpack_next_sequence( # noqa: C901, PLR0912
716733
self, data: str | bytes

src/guidellm/utils/messaging.py

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from multiprocessing.managers import SyncManager
2323
from multiprocessing.synchronize import Event as ProcessingEvent
2424
from threading import Event as ThreadingEvent
25-
from typing import Any, Callable, cast, Generic, List, Protocol, TypeVar
25+
from typing import Any, Callable, Generic, Protocol, TypeVar, cast
2626

2727
import culsans
2828
from pydantic import BaseModel
@@ -50,6 +50,7 @@
5050

5151
CheckStopCallableT = Callable[[bool, int], bool]
5252

53+
5354
class MessagingStopCallback(Protocol):
5455
"""Protocol for evaluating stop conditions in messaging operations."""
5556

@@ -248,14 +249,17 @@ async def stop(self):
248249
if self.shutdown_event is not None:
249250
self.shutdown_event.set()
250251
else:
251-
raise RuntimeError("shutdown_event is not set; was start() not called or is this a redundant stop() call?")
252+
raise RuntimeError(
253+
"shutdown_event is not set; was start() not called or "
254+
"is this a redundant stop() call?"
255+
)
252256
tasks = [self.send_task, self.receive_task]
253-
tasks_to_run: List[asyncio.Task[Any]] = [task for task in tasks if task is not None]
257+
tasks_to_run: list[asyncio.Task[Any]] = [
258+
task for task in tasks if task is not None
259+
]
254260
if len(tasks_to_run) > 0:
255261
with contextlib.suppress(asyncio.CancelledError):
256-
await asyncio.gather(
257-
*tasks_to_run, return_exceptions=True
258-
)
262+
await asyncio.gather(*tasks_to_run, return_exceptions=True)
259263
self.send_task = None
260264
self.receive_task = None
261265
if self.worker_index is None:
@@ -354,7 +358,9 @@ async def get(self, timeout: float | None = None) -> ReceiveMessageT:
354358
:return: Decoded message from the receive buffer
355359
"""
356360
if self.buffer_receive_queue is None:
357-
raise RuntimeError("buffer receive queue is None; check start()/stop() calls")
361+
raise RuntimeError(
362+
"buffer receive queue is None; check start()/stop() calls"
363+
)
358364
return await asyncio.wait_for(
359365
self.buffer_receive_queue.async_get(), timeout=timeout
360366
)
@@ -367,7 +373,9 @@ def get_sync(self, timeout: float | None = None) -> ReceiveMessageT:
367373
:return: Decoded message from the receive buffer
368374
"""
369375
if self.buffer_receive_queue is None:
370-
raise RuntimeError("buffer receive queue is None; check start()/stop() calls")
376+
raise RuntimeError(
377+
"buffer receive queue is None; check start()/stop() calls"
378+
)
371379
if timeout is not None and timeout <= 0:
372380
return self.buffer_receive_queue.get_nowait()
373381
else:
@@ -381,7 +389,9 @@ async def put(self, item: SendMessageT, timeout: float | None = None):
381389
:param timeout: Maximum time to wait for buffer space
382390
"""
383391
if self.buffer_send_queue is None:
384-
raise RuntimeError("buffer receive queue is None; check start()/stop() calls")
392+
raise RuntimeError(
393+
"buffer receive queue is None; check start()/stop() calls"
394+
)
385395
await asyncio.wait_for(self.buffer_send_queue.async_put(item), timeout=timeout)
386396

387397
def put_sync(self, item: SendMessageT, timeout: float | None = None):
@@ -392,7 +402,9 @@ def put_sync(self, item: SendMessageT, timeout: float | None = None):
392402
:param timeout: Maximum time to wait for buffer space, if <=0 uses put_nowait
393403
"""
394404
if self.buffer_send_queue is None:
395-
raise RuntimeError("buffer receive queue is None; check start()/stop() calls")
405+
raise RuntimeError(
406+
"buffer receive queue is None; check start()/stop() calls"
407+
)
396408
if timeout is not None and timeout <= 0:
397409
self.buffer_send_queue.put_nowait(item)
398410
else:
@@ -457,6 +469,7 @@ class InterProcessMessagingQueue(InterProcessMessaging[SendMessageT, ReceiveMess
457469
# Create worker copy for distributed processing
458470
worker_messaging = messaging.create_worker_copy(worker_index=0)
459471
"""
472+
460473
pending_queue: multiprocessing.Queue | queue.Queue[Any] | None
461474
done_queue: multiprocessing.Queue | queue.Queue[Any] | None
462475

@@ -545,15 +558,15 @@ async def stop(self):
545558
with contextlib.suppress(queue.Empty):
546559
while True:
547560
self.pending_queue.get_nowait()
548-
if hasattr(self.pending_queue, 'close'):
561+
if hasattr(self.pending_queue, "close"):
549562
self.pending_queue.close()
550563

551564
if self.done_queue is None:
552565
raise RuntimeError("done_queue is None; was stop() already called?")
553566
with contextlib.suppress(queue.Empty):
554567
while True:
555568
self.done_queue.get_nowait()
556-
if hasattr(self.done_queue, 'close'):
569+
if hasattr(self.done_queue, "close"):
557570
self.done_queue.close()
558571

559572
self.pending_queue = None
@@ -618,7 +631,9 @@ def _send_messages_task_thread( # noqa: C901, PLR0912
618631
item = next(send_items_iter)
619632
else:
620633
if self.buffer_send_queue is None:
621-
raise RuntimeError("buffer_send_queue is None; was stop() already called?")
634+
raise RuntimeError(
635+
"buffer_send_queue is None; was stop() already called?"
636+
)
622637
item = self.buffer_send_queue.sync_get(
623638
timeout=self.poll_interval
624639
)
@@ -632,16 +647,22 @@ def _send_messages_task_thread( # noqa: C901, PLR0912
632647
if self.worker_index is None:
633648
# Main publisher
634649
if self.pending_queue is None:
635-
raise RuntimeError("pending_queue is None; was stop() already called?")
650+
raise RuntimeError(
651+
"pending_queue is None; was stop() already called?"
652+
)
636653
self.pending_queue.put(pending_item, timeout=self.poll_interval)
637654
else:
638655
# Worker
639656
if self.done_queue is None:
640-
raise RuntimeError("done_queue is None; was stop() already called?")
657+
raise RuntimeError(
658+
"done_queue is None; was stop() already called?"
659+
)
641660
self.done_queue.put(pending_item, timeout=self.poll_interval)
642661
if send_items_iter is None:
643662
if self.buffer_send_queue is None:
644-
raise RuntimeError("buffer_send_queue is None; was stop() already called?")
663+
raise RuntimeError(
664+
"buffer_send_queue is None; was stop() already called?"
665+
)
645666
self.buffer_send_queue.task_done()
646667
pending_item = None
647668
except (culsans.QueueFull, queue.Full):
@@ -663,12 +684,16 @@ def _receive_messages_task_thread( # noqa: C901
663684
if self.worker_index is None:
664685
# Main publisher
665686
if self.done_queue is None:
666-
raise RuntimeError("done_queue is None; check start()/stop() calls")
687+
raise RuntimeError(
688+
"done_queue is None; check start()/stop() calls"
689+
)
667690
item = self.done_queue.get(timeout=self.poll_interval)
668691
else:
669692
# Worker
670693
if self.pending_queue is None:
671-
raise RuntimeError("pending_queue is None; check start()/stop() calls")
694+
raise RuntimeError(
695+
"pending_queue is None; check start()/stop() calls"
696+
)
672697
item = self.pending_queue.get(timeout=self.poll_interval)
673698
pending_item = message_encoding.decode(item)
674699
queue_empty_count = 0
@@ -685,8 +710,12 @@ def _receive_messages_task_thread( # noqa: C901
685710
)
686711

687712
if self.buffer_receive_queue is None:
688-
raise RuntimeError("buffer_receive_queue is None; check start()/stop() calls")
689-
self.buffer_receive_queue.sync_put(cast(ReceiveMessageT, received_item))
713+
raise RuntimeError(
714+
"buffer_receive_queue is None; check start()/stop() calls"
715+
)
716+
self.buffer_receive_queue.sync_put(
717+
cast("ReceiveMessageT", received_item)
718+
)
690719
pending_item = None
691720
received_item = None
692721
except (culsans.QueueFull, queue.Full):
@@ -863,9 +892,7 @@ def __init__(
863892

864893
self.pipes: list[tuple[Connection, Connection]]
865894
if pipe is None:
866-
self.pipes = [
867-
self.mp_context.Pipe(duplex=True) for _ in range(num_workers)
868-
]
895+
self.pipes = [self.mp_context.Pipe(duplex=True) for _ in range(num_workers)]
869896
else:
870897
self.pipes = [pipe]
871898

@@ -969,7 +996,7 @@ def create_receive_messages_threads(
969996
)
970997
]
971998

972-
def _send_messages_task_thread( # noqa: C901, PLR0912
999+
def _send_messages_task_thread( # noqa: C901, PLR0912, PLR0915
9731000
self,
9741001
pipe: tuple[Connection, Connection],
9751002
send_items: Iterable[Any] | None,
@@ -1009,7 +1036,9 @@ def _background_pipe_recv():
10091036
item = next(send_items_iter)
10101037
else:
10111038
if self.buffer_send_queue is None:
1012-
raise RuntimeError("buffer_send_queue is None; check start()/stop() calls")
1039+
raise RuntimeError(
1040+
"buffer_send_queue is None; check start()/stop() calls" # noqa: E501
1041+
)
10131042
item = self.buffer_send_queue.sync_get(
10141043
timeout=self.poll_interval
10151044
)
@@ -1028,7 +1057,9 @@ def _background_pipe_recv():
10281057
pipe_item = pending_item
10291058
if send_items_iter is None:
10301059
if self.buffer_send_queue is None:
1031-
raise RuntimeError("buffer_send_queue is None; check start()/stop() calls")
1060+
raise RuntimeError(
1061+
"buffer_send_queue is None; check start()/stop() calls" # noqa: E501
1062+
)
10321063
self.buffer_send_queue.task_done()
10331064
pending_item = None
10341065
except (culsans.QueueFull, queue.Full):
@@ -1071,8 +1102,12 @@ def _receive_messages_task_thread( # noqa: C901
10711102
else receive_callback(pending_item)
10721103
)
10731104
if self.buffer_receive_queue is None:
1074-
raise RuntimeError("buffer receive queue is None; check start()/stop() calls")
1075-
self.buffer_receive_queue.sync_put(cast(ReceiveMessageT, received_item))
1105+
raise RuntimeError(
1106+
"buffer receive queue is None; check start()/stop() calls"
1107+
)
1108+
self.buffer_receive_queue.sync_put(
1109+
cast("ReceiveMessageT", received_item)
1110+
)
10761111
pending_item = None
10771112
received_item = None
10781113
except (culsans.QueueFull, queue.Full):

src/guidellm/utils/pydantic_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from __future__ import annotations
1212

1313
from abc import ABC, abstractmethod
14-
from typing import Any, cast, ClassVar, Generic, TypeVar
14+
from typing import Any, ClassVar, Generic, TypeVar, cast
1515

1616
from pydantic import BaseModel, ConfigDict, Field, GetCoreSchemaHandler
1717
from pydantic_core import CoreSchema, core_schema
@@ -300,7 +300,7 @@ def register_decorator(
300300
super().register_decorator(clazz, name=name)
301301
cls.reload_schema()
302302

303-
return cast(RegisterClassT, clazz)
303+
return cast("RegisterClassT", clazz)
304304

305305
@classmethod
306306
def __get_pydantic_core_schema__(

src/guidellm/utils/registry.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
RegistryObjT = TypeVar("RegistryObjT")
2121
"""Generic type variable for objects managed by the registry system."""
22-
RegisterT = TypeVar("RegisterT", bound=type) # Must be bound to type to ensure __name__ is available.
22+
RegisterT = TypeVar(
23+
"RegisterT", bound=type
24+
) # Must be bound to type to ensure __name__ is available.
2325
"""Generic type variable for the args and return values within the registry."""
2426

2527

0 commit comments

Comments
 (0)