Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyserial: make serial.threaded.ReaderThread generic in the type of Protocol #13425

Merged
merged 6 commits into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions stubs/pyserial/serial/threaded/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@ import threading
from _typeshed import ReadableBuffer
from collections.abc import Callable
from types import TracebackType
from typing import Generic, TypeVar
from typing_extensions import Self

from serial import Serial

_P = TypeVar("_P", bound=Protocol, default="Protocol") # noqa: Y020

class Protocol:
def connection_made(self, transport: ReaderThread) -> None: ...
def connection_made(self, transport: ReaderThread[Self]) -> None: ...
def data_received(self, data: bytes) -> None: ...
def connection_lost(self, exc: BaseException | None) -> None: ...

class Packetizer(Protocol):
TERMINATOR: bytes
buffer: bytearray
transport: ReaderThread | None
transport: ReaderThread[Self] | None
def handle_packet(self, packet: bytes) -> None: ...

class FramedPacket(Protocol):
START: bytes
STOP: bytes
packet: bytearray
in_packet: bool
transport: ReaderThread | None
transport: ReaderThread[Self] | None
def handle_packet(self, packet: bytes) -> None: ...
def handle_out_of_packet_data(self, data: bytes) -> None: ...

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

class ReaderThread(threading.Thread):
class ReaderThread(threading.Thread, Generic[_P]):
serial: Serial
protocol_factory: Callable[[], Protocol]
protocol_factory: Callable[[], _P]
alive: bool
protocol: Protocol
def __init__(self, serial_instance: Serial, protocol_factory: Callable[[], Protocol]) -> None: ...
protocol: _P
def __init__(self, serial_instance: Serial, protocol_factory: Callable[[], _P]) -> None: ...
def stop(self) -> None: ...
def write(self, data: ReadableBuffer) -> int: ...
def close(self) -> None: ...
def connect(self) -> tuple[Self, Protocol]: ...
def __enter__(self) -> Protocol: ...
def connect(self) -> tuple[Self, _P]: ...
def __enter__(self) -> _P: ...
def __exit__(
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None, /
) -> None: ...
Loading