|
1 | 1 | import io |
| 2 | +from _typeshed import ReadableBuffer, WriteableBuffer |
| 3 | +from abc import abstractmethod |
2 | 4 | from collections.abc import Callable, Generator |
3 | 5 | from typing import Any |
4 | 6 | from typing_extensions import Final |
@@ -63,7 +65,21 @@ class SerialBase(io.RawIOBase): |
63 | 65 | inter_byte_timeout: float | None = ..., |
64 | 66 | exclusive: float | None = ..., |
65 | 67 | ) -> None: ... |
66 | | - def read(self, __size: int = ...) -> bytes: ... # same as io.RawIOBase.read but always returns bytes |
| 68 | + |
| 69 | + # Return type: |
| 70 | + # ------------ |
| 71 | + # `io.RawIOBase`, the super class, declares the return type of read as `-> bytes | None`. |
| 72 | + # `SerialBase` does not define `read` at runtime but REQUIRES subclasses to implement it and |
| 73 | + # require it to return `bytes`. |
| 74 | + # Abstract: |
| 75 | + # --------- |
| 76 | + # `io.RawIOBase` implements `read` in terms of `readinto`. `SerialBase` implements `readinto` |
| 77 | + # in terms of `read`. If subclasses do not implement `read`, any call to `read` or `read_into` |
| 78 | + # will fail at runtime with a `RecursionError`. |
| 79 | + @abstractmethod |
| 80 | + def read(self, __size: int = -1) -> bytes: ... |
| 81 | + @abstractmethod |
| 82 | + def write(self, __b: ReadableBuffer) -> int | None: ... |
67 | 83 | @property |
68 | 84 | def port(self) -> str | None: ... |
69 | 85 | @port.setter |
@@ -130,6 +146,7 @@ class SerialBase(io.RawIOBase): |
130 | 146 | def rs485_mode(self, rs485_settings: RS485Settings | None) -> None: ... |
131 | 147 | def get_settings(self) -> dict[str, Any]: ... |
132 | 148 | def apply_settings(self, d: dict[str, Any]) -> None: ... |
| 149 | + def readinto(self, __buffer: WriteableBuffer) -> int: ... # returns int unlike `io.RawIOBase` |
133 | 150 | def send_break(self, duration: float = ...) -> None: ... |
134 | 151 | def read_all(self) -> bytes | None: ... |
135 | 152 | def read_until(self, expected: bytes = ..., size: int | None = ...) -> bytes: ... |
|
0 commit comments