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

Add stubs for the pyserial package #9347

Merged
merged 11 commits into from
Dec 22, 2022
82 changes: 82 additions & 0 deletions stubs/pyserial/@tests/stubtest_allowlist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Error: failed to import
# =======================
serial.__main__ # SystemExit
serial.serialcli # (IronPython) ModuleNotFoundError: No module named 'System'
serial.serialjava # No Java Communications API implementation found

# Error: is not present at runtime
# ================================
serial.urlhandler.protocol_spy.FormatLog # Class exists!
hamdanal marked this conversation as resolved.
Show resolved Hide resolved
serial.urlhandler.protocol_spy.FormatLogHex # Class exists!
serial.tools.miniterm.Console.__init__ # Argument exists!
serial.tools.miniterm.Console.sigint # Method exists!
serial.tools.miniterm.ConsoleBase.__init__ # Argument exists!
serial.tools.miniterm.main # Argument exists!

# Error: is inconsistent
# ======================
# These are positional only argument in the stub because they inherit from io.RawIOBase
# but at runtime they are normal arguments that don't have consistent names.
serial.Serial.read
serial.Serial.write
serial.SerialBase.readinto
serial.serialutil.SerialBase.readinto
serial.rfc2217.Serial.read
serial.rfc2217.Serial.write
serial.rs485.RS485.write
serial.urlhandler.protocol_cp2110.Serial.read
serial.urlhandler.protocol_cp2110.Serial.write
serial.urlhandler.protocol_loop.Serial.read
serial.urlhandler.protocol_loop.Serial.write
serial.urlhandler.protocol_rfc2217.Serial.read
serial.urlhandler.protocol_rfc2217.Serial.write
serial.urlhandler.protocol_socket.Serial.read
serial.urlhandler.protocol_socket.Serial.write
serial.urlhandler.protocol_spy.Serial.read
serial.urlhandler.protocol_spy.Serial.write

# Error: is not present in stub
# =============================
# Python 2 compatibility
serial.basestring
serial.serialutil.basestring
serial.serialutil.iterbytes
serial.serialutil.to_bytes

# Deprecated aliases
serial.SerialBase.applySettingsDict
serial.SerialBase.flushInput
serial.SerialBase.flushOutput
serial.SerialBase.getCD
serial.SerialBase.getCTS
serial.SerialBase.getDSR
serial.SerialBase.getRI
serial.SerialBase.getSettingsDict
serial.SerialBase.inWaiting
serial.SerialBase.interCharTimeout
serial.SerialBase.isOpen
serial.SerialBase.sendBreak
serial.SerialBase.setDTR
serial.SerialBase.setPort
serial.SerialBase.setRTS
serial.SerialBase.writeTimeout
serial.serialutil.SerialBase.applySettingsDict
serial.serialutil.SerialBase.flushInput
serial.serialutil.SerialBase.flushOutput
serial.serialutil.SerialBase.getCD
serial.serialutil.SerialBase.getCTS
serial.serialutil.SerialBase.getDSR
serial.serialutil.SerialBase.getRI
serial.serialutil.SerialBase.getSettingsDict
serial.serialutil.SerialBase.inWaiting
serial.serialutil.SerialBase.interCharTimeout
serial.serialutil.SerialBase.isOpen
serial.serialutil.SerialBase.sendBreak
serial.serialutil.SerialBase.setDTR
serial.serialutil.SerialBase.setPort
serial.serialutil.SerialBase.setRTS
serial.serialutil.SerialBase.writeTimeout

# Private aliases
serial.serialposix.plat
serial.tools.list_ports_posix.plat
21 changes: 21 additions & 0 deletions stubs/pyserial/@tests/stubtest_allowlist_darwin.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Error: failed to import
# =======================
serial.serialwin32 # Windows only
serial.win32 # Windows only
serial.tools.list_ports_linux # Linux only
serial.tools.list_ports_windows # Windows only

# Error: is inconsistent
# ======================
# These are positional only argument in the stub because they inherit from io.RawIOBase
# but at runtime they are normal arguments that don't have consistent names.
serial.PosixPollSerial.read
serial.VTIMESerial.read
serial.serialposix.Serial.read
serial.serialposix.Serial.write
serial.serialposix.PosixPollSerial.read
serial.serialposix.VTIMESerial.read

# Error: is not present at runtime
# ================================
serial.serialposix.BAUDRATE_OFFSET # It exists
21 changes: 21 additions & 0 deletions stubs/pyserial/@tests/stubtest_allowlist_linux.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Error: failed to import
# =======================
serial.serialwin32 # Windows only
serial.win32 # Windows only
serial.tools.list_ports_osx # Mac only
serial.tools.list_ports_windows # Windows only

# Error: is inconsistent
# ======================
# These are positional only argument in the stub because they inherit from io.RawIOBase
# but at runtime they are normal arguments that don't have consistent names.
serial.PosixPollSerial.read
serial.VTIMESerial.read
serial.serialposix.Serial.read
serial.serialposix.Serial.write
serial.serialposix.PosixPollSerial.read
serial.serialposix.VTIMESerial.read

# Error: is not present at runtime
# ================================
serial.serialposix.BAUDRATE_OFFSET # It exists
12 changes: 12 additions & 0 deletions stubs/pyserial/@tests/stubtest_allowlist_win32.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Error: failed to import
# =======================
serial.serialposix # Posix only
serial.tools.list_ports_osx # Mac only
serial.tools.list_ports_linux # Linux only

# Error: is inconsistent
# ======================
# These are positional only argument in the stub because they inherit from io.RawIOBase
# but at runtime they are normal arguments that don't have consistent names.
serial.serialwin32.Serial.read
serial.serialwin32.Serial.write
10 changes: 10 additions & 0 deletions stubs/pyserial/METADATA.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version = "3.5.*"

[tool.stubtest]
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
# Skip stubtest because it crashes with `SystemExit` when trying to import `serial.__main__`.
# This has been fixed in https://github.com/python/mypy/pull/14284.
# TODO: Remove skip once mypy 1.0 is released with the fix.
skip = true
ignore_missing_stub = false
platforms = ["linux", "win32", "darwin"]
extras = ["cp2110"]
30 changes: 30 additions & 0 deletions stubs/pyserial/serial/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sys

from serial.serialutil import *

if sys.platform == "win32":
from serial.serialwin32 import Serial as Serial
else:
from serial.serialposix import PosixPollSerial as PosixPollSerial, Serial as Serial, VTIMESerial as VTIMESerial
# TODO: java? cli? These platforms raise flake8-pyi Y008. Should they be included with a noqa?

__version__: str
VERSION: str
protocol_handler_packages: list[str]

def serial_for_url(
url: str | None,
baudrate: int = ...,
bytesize: int = ...,
parity: str = ...,
stopbits: float = ...,
timeout: float | None = ...,
xonxoff: bool = ...,
rtscts: bool = ...,
write_timeout: float | None = ...,
dsrdtr: bool = ...,
inter_byte_timeout: float | None = ...,
exclusive: float | None = ...,
*,
do_not_open: bool = ...,
) -> Serial: ...
1 change: 1 addition & 0 deletions stubs/pyserial/serial/__main__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from serial.tools import miniterm as miniterm
184 changes: 184 additions & 0 deletions stubs/pyserial/serial/rfc2217.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
import logging
from collections.abc import Callable, Generator
from typing import Any

from serial.serialutil import SerialBase

LOGGER_LEVELS: dict[str, int]
SE: bytes
NOP: bytes
DM: bytes
BRK: bytes
IP: bytes
AO: bytes
AYT: bytes
EC: bytes
EL: bytes
GA: bytes
SB: bytes
WILL: bytes
WONT: bytes
DO: bytes
DONT: bytes
IAC: bytes
IAC_DOUBLED: bytes
BINARY: bytes
ECHO: bytes
SGA: bytes
COM_PORT_OPTION: bytes
SET_BAUDRATE: bytes
SET_DATASIZE: bytes
SET_PARITY: bytes
SET_STOPSIZE: bytes
SET_CONTROL: bytes
NOTIFY_LINESTATE: bytes
NOTIFY_MODEMSTATE: bytes
FLOWCONTROL_SUSPEND: bytes
FLOWCONTROL_RESUME: bytes
SET_LINESTATE_MASK: bytes
SET_MODEMSTATE_MASK: bytes
PURGE_DATA: bytes
SERVER_SET_BAUDRATE: bytes
SERVER_SET_DATASIZE: bytes
SERVER_SET_PARITY: bytes
SERVER_SET_STOPSIZE: bytes
SERVER_SET_CONTROL: bytes
SERVER_NOTIFY_LINESTATE: bytes
SERVER_NOTIFY_MODEMSTATE: bytes
SERVER_FLOWCONTROL_SUSPEND: bytes
SERVER_FLOWCONTROL_RESUME: bytes
SERVER_SET_LINESTATE_MASK: bytes
SERVER_SET_MODEMSTATE_MASK: bytes
SERVER_PURGE_DATA: bytes
RFC2217_ANSWER_MAP: dict[bytes, bytes]
SET_CONTROL_REQ_FLOW_SETTING: bytes
SET_CONTROL_USE_NO_FLOW_CONTROL: bytes
SET_CONTROL_USE_SW_FLOW_CONTROL: bytes
SET_CONTROL_USE_HW_FLOW_CONTROL: bytes
SET_CONTROL_REQ_BREAK_STATE: bytes
SET_CONTROL_BREAK_ON: bytes
SET_CONTROL_BREAK_OFF: bytes
SET_CONTROL_REQ_DTR: bytes
SET_CONTROL_DTR_ON: bytes
SET_CONTROL_DTR_OFF: bytes
SET_CONTROL_REQ_RTS: bytes
SET_CONTROL_RTS_ON: bytes
SET_CONTROL_RTS_OFF: bytes
SET_CONTROL_REQ_FLOW_SETTING_IN: bytes
SET_CONTROL_USE_NO_FLOW_CONTROL_IN: bytes
SET_CONTROL_USE_SW_FLOW_CONTOL_IN: bytes
SET_CONTROL_USE_HW_FLOW_CONTOL_IN: bytes
SET_CONTROL_USE_DCD_FLOW_CONTROL: bytes
SET_CONTROL_USE_DTR_FLOW_CONTROL: bytes
SET_CONTROL_USE_DSR_FLOW_CONTROL: bytes
LINESTATE_MASK_TIMEOUT: int
LINESTATE_MASK_SHIFTREG_EMPTY: int
LINESTATE_MASK_TRANSREG_EMPTY: int
LINESTATE_MASK_BREAK_DETECT: int
LINESTATE_MASK_FRAMING_ERROR: int
LINESTATE_MASK_PARTIY_ERROR: int
LINESTATE_MASK_OVERRUN_ERROR: int
LINESTATE_MASK_DATA_READY: int
MODEMSTATE_MASK_CD: int
MODEMSTATE_MASK_RI: int
MODEMSTATE_MASK_DSR: int
MODEMSTATE_MASK_CTS: int
MODEMSTATE_MASK_CD_CHANGE: int
MODEMSTATE_MASK_RI_CHANGE: int
MODEMSTATE_MASK_DSR_CHANGE: int
MODEMSTATE_MASK_CTS_CHANGE: int
PURGE_RECEIVE_BUFFER: bytes
PURGE_TRANSMIT_BUFFER: bytes
PURGE_BOTH_BUFFERS: bytes
RFC2217_PARITY_MAP: dict[str, int]
RFC2217_REVERSE_PARITY_MAP: dict[int, str]
RFC2217_STOPBIT_MAP: dict[int | float, int]
RFC2217_REVERSE_STOPBIT_MAP: dict[int, int | float]
M_NORMAL: int
M_IAC_SEEN: int
M_NEGOTIATE: int
REQUESTED: str
ACTIVE: str
INACTIVE: str
REALLY_INACTIVE: str

class TelnetOption:
connection: Serial
name: str
option: bytes
send_yes: bytes
send_no: bytes
ack_yes: bytes
ack_no: bytes
state: str
active: bool
activation_callback: Callable[[], Any]

def __init__(
self,
connection: Serial,
name: str,
option: bytes,
send_yes: bytes,
send_no: bytes,
ack_yes: bytes,
ack_no: bytes,
initial_state: str,
activation_callback: Callable[[], Any] | None = ...,
) -> None: ...
def process_incoming(self, command: bytes) -> None: ...

class TelnetSubnegotiation:
connection: Serial
name: str
option: bytes
value: bytes | None
ack_option: bytes
state: str
def __init__(self, connection: Serial, name: str, option: bytes, ack_option: bytes | None = ...) -> None: ...
def set(self, value: bytes) -> None: ...
def is_ready(self) -> bool: ...
@property
def active(self) -> bool: ...
def wait(self, timeout: float = ...) -> None: ...
def check_answer(self, suboption: bytes) -> None: ...

class Serial(SerialBase):
logger: logging.Logger | None
def open(self) -> None: ...
def from_url(self, url: str) -> tuple[str, int]: ...
@property
def in_waiting(self) -> int: ...
def reset_input_buffer(self) -> None: ...
def reset_output_buffer(self) -> None: ...
@property
def cts(self) -> bool: ...
@property
def dsr(self) -> bool: ...
@property
def ri(self) -> bool: ...
@property
def cd(self) -> bool: ...
def telnet_send_option(self, action: bytes, option: bytes) -> None: ...
def rfc2217_send_subnegotiation(self, option: bytes, value: bytes = ...) -> None: ...
def rfc2217_send_purge(self, value: bytes) -> None: ...
def rfc2217_set_control(self, value: bytes) -> None: ...
def rfc2217_flow_server_ready(self) -> None: ...
def get_modem_state(self) -> int: ...

class PortManager:
serial: Serial
connection: Serial
logger: logging.Logger | None
mode: int
suboption: bytes | None
telnet_command: bytes | None
modemstate_mask: int
last_modemstate: int | None
linstate_mask: int
def __init__(self, serial_port: Serial, connection: Serial, logger: logging.Logger | None = ...) -> None: ...
def telnet_send_option(self, action: bytes, option: bytes) -> None: ...
def rfc2217_send_subnegotiation(self, option: bytes, value: bytes = ...) -> None: ...
def check_modem_lines(self, force_notification: bool = ...) -> None: ...
def escape(self, data: bytes) -> Generator[bytes, None, None]: ...
def filter(self, data: bytes) -> Generator[bytes, None, None]: ...
18 changes: 18 additions & 0 deletions stubs/pyserial/serial/rs485.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import serial

class RS485Settings:
rts_level_for_tx: bool
rts_level_for_rx: bool
loopback: bool
delay_before_tx: float | None
delay_before_rx: float | None
def __init__(
self,
rts_level_for_tx: bool = ...,
rts_level_for_rx: bool = ...,
loopback: bool = ...,
delay_before_tx: float | None = ...,
delay_before_rx: float | None = ...,
) -> None: ...

class RS485(serial.Serial): ...
Loading