Skip to content

Commit f8add36

Browse files
authored
pyserial: Fix read and write methods (#9825)
1 parent 4b9c1b9 commit f8add36

13 files changed

+44
-19
lines changed

stubs/pyserial/@tests/stubtest_allowlist.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,15 @@ serial.serialjava # No Java Communications API implementation found
88
# ======================
99
# These are positional only argument in the stub because they inherit from io.RawIOBase
1010
# but at runtime they are normal arguments that don't have consistent names.
11-
serial.Serial.read
1211
serial.Serial.write
1312
serial.SerialBase.readinto
1413
serial.serialutil.SerialBase.readinto
15-
serial.rfc2217.Serial.read
1614
serial.rfc2217.Serial.write
1715
serial.rs485.RS485.write
18-
serial.urlhandler.protocol_cp2110.Serial.read
1916
serial.urlhandler.protocol_cp2110.Serial.write
20-
serial.urlhandler.protocol_loop.Serial.read
2117
serial.urlhandler.protocol_loop.Serial.write
22-
serial.urlhandler.protocol_rfc2217.Serial.read
2318
serial.urlhandler.protocol_rfc2217.Serial.write
24-
serial.urlhandler.protocol_socket.Serial.read
2519
serial.urlhandler.protocol_socket.Serial.write
26-
serial.urlhandler.protocol_spy.Serial.read
2720
serial.urlhandler.protocol_spy.Serial.write
2821

2922
# Error: is not present in stub

stubs/pyserial/@tests/stubtest_allowlist_darwin.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ serial.tools.list_ports_windows # Windows only
99
# Methods defined with positional-only argument in the stub because they inherit from
1010
# io.RawIOBase but at runtime they are normal arguments that don't have consistent
1111
# names.
12-
serial.PosixPollSerial.read
13-
serial.VTIMESerial.read
14-
serial.serialposix.Serial.read
1512
serial.serialposix.Serial.write
16-
serial.serialposix.PosixPollSerial.read
17-
serial.serialposix.VTIMESerial.read
1813

1914
# intended to be private aliases
2015
serial.tools.list_ports_posix.plat

stubs/pyserial/@tests/stubtest_allowlist_linux.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ serial.tools.list_ports_windows # Windows only
1010
# Methods defined with positional-only argument in the stub because they inherit from
1111
# io.RawIOBase but at runtime they are normal arguments that don't have consistent
1212
# names.
13-
serial.PosixPollSerial.read
14-
serial.VTIMESerial.read
15-
serial.serialposix.Serial.read
1613
serial.serialposix.Serial.write
17-
serial.serialposix.PosixPollSerial.read
18-
serial.serialposix.VTIMESerial.read
1914

2015
# Error: is missing from the stub (intended to be private aliases)
2116
# ================================================================

stubs/pyserial/@tests/stubtest_allowlist_win32.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ serial.tools.list_ports_posix # Posix only
99
# Methods defined with positional-only argument in the stub because they inherit from
1010
# io.RawIOBase but at runtime they are normal arguments that don't have consistent
1111
# names.
12-
serial.serialwin32.Serial.read
1312
serial.serialwin32.Serial.write

stubs/pyserial/serial/rfc2217.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from _typeshed import ReadableBuffer
23
from collections.abc import Callable, Generator
34
from typing import Any
45

@@ -149,6 +150,8 @@ class Serial(SerialBase):
149150
def from_url(self, url: str) -> tuple[str, int]: ...
150151
@property
151152
def in_waiting(self) -> int: ...
153+
def read(self, size: int = 1) -> bytes: ...
154+
def write(self, __b: ReadableBuffer) -> int | None: ...
152155
def reset_input_buffer(self) -> None: ...
153156
def reset_output_buffer(self) -> None: ...
154157
@property

stubs/pyserial/serial/serialcli.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import ReadableBuffer
12
from typing import Any
23

34
from serial.serialutil import *
@@ -10,6 +11,8 @@ class Serial(SerialBase):
1011
def open(self) -> None: ...
1112
@property
1213
def in_waiting(self) -> int: ...
14+
def read(self, size: int = 1) -> bytes: ...
15+
def write(self, __b: ReadableBuffer) -> int | None: ...
1316
def reset_input_buffer(self) -> None: ...
1417
def reset_output_buffer(self) -> None: ...
1518
@property

stubs/pyserial/serial/serialjava.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import ReadableBuffer
12
from collections.abc import Iterable
23
from typing import Any
34

@@ -15,6 +16,8 @@ class Serial(SerialBase):
1516
def open(self) -> None: ...
1617
@property
1718
def in_waiting(self) -> int: ...
19+
def read(self, size: int = 1) -> bytes: ...
20+
def write(self, __b: ReadableBuffer) -> int | None: ...
1821
def reset_input_buffer(self) -> None: ...
1922
def reset_output_buffer(self) -> None: ...
2023
@property

stubs/pyserial/serial/serialposix.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from _typeshed import ReadableBuffer
23
from typing_extensions import Never
34

45
from serial.serialutil import SerialBase
@@ -64,8 +65,10 @@ class Serial(SerialBase, PlatformSpecific):
6465
def open(self) -> None: ...
6566
@property
6667
def in_waiting(self) -> int: ...
68+
def read(self, size: int = 1) -> bytes: ...
6769
def cancel_read(self) -> None: ...
6870
def cancel_write(self) -> None: ...
71+
def write(self, __b: ReadableBuffer) -> int | None: ...
6972
def reset_input_buffer(self) -> None: ...
7073
def reset_output_buffer(self) -> None: ...
7174
def send_break(self, duration: float = ...) -> None: ...

stubs/pyserial/serial/serialutil.pyi

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import io
2+
from _typeshed import ReadableBuffer, WriteableBuffer
3+
from abc import abstractmethod
24
from collections.abc import Callable, Generator
35
from typing import Any
46
from typing_extensions import Final
@@ -63,7 +65,21 @@ class SerialBase(io.RawIOBase):
6365
inter_byte_timeout: float | None = ...,
6466
exclusive: float | None = ...,
6567
) -> 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: ...
6783
@property
6884
def port(self) -> str | None: ...
6985
@port.setter
@@ -130,6 +146,7 @@ class SerialBase(io.RawIOBase):
130146
def rs485_mode(self, rs485_settings: RS485Settings | None) -> None: ...
131147
def get_settings(self) -> dict[str, Any]: ...
132148
def apply_settings(self, d: dict[str, Any]) -> None: ...
149+
def readinto(self, __buffer: WriteableBuffer) -> int: ... # returns int unlike `io.RawIOBase`
133150
def send_break(self, duration: float = ...) -> None: ...
134151
def read_all(self) -> bytes | None: ...
135152
def read_until(self, expected: bytes = ..., size: int | None = ...) -> bytes: ...

stubs/pyserial/serial/serialwin32.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
from _typeshed import ReadableBuffer
2+
13
from serial.serialutil import SerialBase
24

35
class Serial(SerialBase):
46
def open(self) -> None: ...
57
@property
68
def in_waiting(self) -> int: ...
9+
def read(self, size: int = 1) -> bytes: ...
10+
def write(self, __b: ReadableBuffer) -> int | None: ...
711
def reset_input_buffer(self) -> None: ...
812
def reset_output_buffer(self) -> None: ...
913
@property

0 commit comments

Comments
 (0)