Skip to content
Open
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
7 changes: 6 additions & 1 deletion meshtastic/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
import packaging.version as pkg_version
import requests
import serial # type: ignore[import-untyped]
import serial.tools.list_ports # type: ignore[import-untyped]
try:
import serial.tools.list_ports # type: ignore[import-untyped]
except ImportError as e:
print("Error: ", e)
print("Error: Cannot list ports on this platform")
print("Warning: Skipping import serial.tools.list_ports")
Comment on lines +22 to +27

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Define a fallback for failed imports.

When this import raises ImportError, the handler does not initialize a usable fallback. However, findPorts() still calls serial.tools.list_ports.comports() at Line 162. On an unsupported platform, findPorts() can raise NameError or AttributeError instead of returning no ports.

Store a sentinel such as list_ports = None, check it at the start of findPorts(), and return an empty list before accessing the unavailable module. Add a regression test for the failed-import path.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@meshtastic/util.py` around lines 22 - 27, Initialize list_ports to None in
the ImportError handler, then update findPorts() to return an empty list before
calling list_ports.comports() when the fallback is unavailable. Preserve normal
port discovery when the import succeeds, and add a regression test covering the
failed-import path.


from meshtastic.supported_device import supported_devices
from meshtastic.version import get_active_version
Expand Down