Typing stubs for pyserial
types-pyserial provides static type annotations for the popular pyserial library. It is part of the typeshed project, which centrally maintains type stubs for the Python standard library and many third-party packages. These stubs are used by static type checkers like MyPy or Pyright to verify code correctness without running it, enhancing development and catching potential issues early. The package is regularly updated, often daily, to reflect changes and improvements in the corresponding runtime library, pyserial. This version aims to provide accurate annotations for pyserial==3.5.*.
Warnings
- gotcha The PyPI package name is `types-pyserial`, but the runtime library is imported as `serial` (i.e., `import serial`). Confusing these can lead to `ModuleNotFoundError` or incorrect type checking behavior if an unrelated `serial` package is installed.
- breaking Type stub versions are primarily tied to the major.minor version of the runtime package they describe. For example, `types-pyserial==3.5.*` targets `pyserial==3.5.*`. If `pyserial` updates its API (especially in a major or minor release), the corresponding `types-pyserial` stubs will also update, potentially causing new type-checking errors if your application's code relies on older APIs.
- gotcha `types-pyserial` is a stub-only package and provides no runtime functionality. It is solely used by static type checkers. If `pyserial` (the actual runtime library) is not installed, your code will fail at runtime with an `ImportError`, even if `types-pyserial` is present.
- gotcha Typeshed stub packages are automatically released, sometimes as frequently as once a day. While this ensures up-to-date annotations, it means `types-pyserial` can update more frequently than your runtime `pyserial` package or your type checker, potentially introducing new type errors due to changes in stub definitions or reliance on newer typing features.
- gotcha Typeshed (and thus `types-pyserial`) generally supports a range of recent Python versions. Ensure your Python environment is within the supported range (e.g., 3.10 to 3.14 for current typeshed) to guarantee correct stub interpretation by your type checker.
Install
-
pip install types-pyserial
Imports
- Serial
from serial import Serial
Quickstart
import os
from serial import Serial
# Replace with your actual serial port name (e.g., 'COM1' on Windows, '/dev/ttyUSB0' on Linux)
SERIAL_PORT = os.environ.get('SERIAL_PORT', '/dev/ttyUSB0')
BAUDRATE = 9600
def communicate_with_serial(port: str, baud: int) -> None:
ser: Serial | None = None
try:
# The `Serial` class is correctly typed by types-pyserial
ser = Serial(port, baud)
print(f"Opened serial port {port} at {baud} baud.")
# Example: Write data
message_to_send = b"Hello, device!\n"
ser.write(message_to_send)
print(f"Sent: {message_to_send.decode().strip()}")
# Example: Read data (with a timeout)
ser.timeout = 1 # seconds
received_data = ser.readline()
if received_data:
print(f"Received: {received_data.decode().strip()}")
else:
print("No data received within timeout.")
except Exception as e:
print(f"Error during serial communication: {e}")
finally:
if ser and ser.is_open:
ser.close()
print(f"Closed serial port {port}.")
if __name__ == "__main__":
communicate_with_serial(SERIAL_PORT, BAUDRATE)