Skip to content

Commit 3580566

Browse files
authored
pyserial: make serial.threaded.ReaderThread generic in the type of Protocol (#13425)
1 parent c546278 commit 3580566

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

stubs/pyserial/serial/threaded/__init__.pyi

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,30 @@ import threading
22
from _typeshed import ReadableBuffer
33
from collections.abc import Callable
44
from types import TracebackType
5+
from typing import Generic, TypeVar
56
from typing_extensions import Self
67

78
from serial import Serial
89

10+
_P = TypeVar("_P", bound=Protocol, default="Protocol") # noqa: Y020
11+
912
class Protocol:
10-
def connection_made(self, transport: ReaderThread) -> None: ...
13+
def connection_made(self, transport: ReaderThread[Self]) -> None: ...
1114
def data_received(self, data: bytes) -> None: ...
1215
def connection_lost(self, exc: BaseException | None) -> None: ...
1316

1417
class Packetizer(Protocol):
1518
TERMINATOR: bytes
1619
buffer: bytearray
17-
transport: ReaderThread | None
20+
transport: ReaderThread[Self] | None
1821
def handle_packet(self, packet: bytes) -> None: ...
1922

2023
class FramedPacket(Protocol):
2124
START: bytes
2225
STOP: bytes
2326
packet: bytearray
2427
in_packet: bool
25-
transport: ReaderThread | None
28+
transport: ReaderThread[Self] | None
2629
def handle_packet(self, packet: bytes) -> None: ...
2730
def handle_out_of_packet_data(self, data: bytes) -> None: ...
2831

@@ -32,17 +35,17 @@ class LineReader(Packetizer):
3235
def handle_line(self, line: str) -> None: ...
3336
def write_line(self, text: str) -> None: ...
3437

35-
class ReaderThread(threading.Thread):
38+
class ReaderThread(threading.Thread, Generic[_P]):
3639
serial: Serial
37-
protocol_factory: Callable[[], Protocol]
40+
protocol_factory: Callable[[], _P]
3841
alive: bool
39-
protocol: Protocol
40-
def __init__(self, serial_instance: Serial, protocol_factory: Callable[[], Protocol]) -> None: ...
42+
protocol: _P
43+
def __init__(self, serial_instance: Serial, protocol_factory: Callable[[], _P]) -> None: ...
4144
def stop(self) -> None: ...
4245
def write(self, data: ReadableBuffer) -> int: ...
4346
def close(self) -> None: ...
44-
def connect(self) -> tuple[Self, Protocol]: ...
45-
def __enter__(self) -> Protocol: ...
47+
def connect(self) -> tuple[Self, _P]: ...
48+
def __enter__(self) -> _P: ...
4649
def __exit__(
4750
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None, /
4851
) -> None: ...

0 commit comments

Comments
 (0)