Skip to content

Add a simple async API#79

Merged
puddly merged 9 commits intodevfrom
puddly/async-sync-api
May 5, 2026
Merged

Add a simple async API#79
puddly merged 9 commits intodevfrom
puddly/async-sync-api

Conversation

@puddly
Copy link
Copy Markdown
Owner

@puddly puddly commented May 5, 2026

This PR introduces serialx.async_serial_for_url, a simple async API:

import serialx

async with serialx.async_serial_for_url(
    "/dev/serial/by-id/port", baudrate=115200,
) as serial:
    data = await serial.readexactly(5)
    serial.write(b"test")
    await serial.flush()

    await serial.set_modem_pins(rts=True, dtr=True)
    pins = await serial.get_modem_pins()
    assert pins.rts is serialx.PinState.HIGH

This makes migration of existing sync code straightforward, as often times even simple scripts graduate to needing background tasks, timeouts, and so on. I've added some documentation to steer users to either use this async API or to use the low-level serialx.create_serial_connection API. The intermediate serialx.open_serial_connection returning StreamReader and StreamWriter is clunky to use and error-prone, especially when it comes to open/close.

Many unit tests have also been simplified to take advantage of this simpler API.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 5, 2026

Codecov Report

❌ Patch coverage is 96.92308% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.08%. Comparing base (b4a65e7) to head (9e2c313).
⚠️ Report is 3 commits behind head on dev.

Files with missing lines Patch % Lines
serialx/async_serial.py 96.19% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev      #79      +/-   ##
==========================================
+ Coverage   90.71%   91.08%   +0.37%     
==========================================
  Files          22       22              
  Lines        3372     3748     +376     
==========================================
+ Hits         3059     3414     +355     
- Misses        313      334      +21     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a new simplified asyncio-facing API (serialx.async_serial_for_url) that returns an AsyncSerial object with a sync-like method surface, and updates tests + documentation to prefer this higher-level async interface over the existing (StreamReader, StreamWriter) pattern.

Changes:

  • Added AsyncSerial and the async_serial_for_url() factory, and exported them from serialx.
  • Refactored tests to use AsyncSerial (including cross-loop ESPHome scenarios) and simplified test helpers.
  • Updated/expanded docs to steer users toward the simplified async API while keeping the streams/transport APIs documented.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
serialx/async_serial.py Introduces AsyncSerial and async_serial_for_url(); implements read/write/flush and modem-pin helpers.
serialx/__init__.py Exports AsyncSerial and async_serial_for_url from the package root.
serialx/common.py Adds ConnectKwargs TypedDict and updates BaseSerialTransport.connect/_connect typing to use Unpack.
serialx/descriptor_transport.py Adjusts _connect signature to accept path + ConnectKwargs.
serialx/platforms/serial_esphome.py Updates transport _connect signature and forwards path explicitly.
serialx/platforms/serial_rfc2217/__init__.py Updates transport _connect signature and forwards path explicitly into RFC2217Serial.
serialx/platforms/serial_win32.py Updates transport _connect signature and typing around forwarded connect kwargs.
tests/common.py Replaces stream reader/writer helpers with async_create_serial_pair() yielding AsyncSerial objects.
tests/test_async_transports.py Migrates tests to AsyncSerial and the new serial-pair helper.
tests/test_serial_esphome.py Migrates ESPHome tests from streams API to AsyncSerial, including cross-loop coverage.
README.md Documents the simplified async API and keeps the streams/transport alternatives.
docs/usage.md Adds a “simple async API” section and positions streams/transport as alternatives.
docs/quickstart.md Updates quickstart async section to use async_serial_for_url.
docs/index.md Adds a new how-to page entry for async serial.
docs/how-to/async-serial.md New how-to describing patterns and tradeoffs for the simplified async API.
docs/how-to/pyserial-migration.md Adds guidance for migrating sync serial_for_url code to the new async API.
docs/api/platforms/posix.md Removes an extra autofunction block from the POSIX docs page.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread serialx/async_serial.py Outdated
Comment thread docs/quickstart.md
Comment thread docs/how-to/async-serial.md
Comment thread docs/how-to/async-serial.md
Comment thread docs/how-to/async-serial.md
Comment thread docs/usage.md
Comment thread README.md Outdated
Comment thread tests/test_async_transports.py Outdated
@puddly puddly force-pushed the puddly/async-sync-api branch from 86885e5 to 0214792 Compare May 5, 2026 17:09
@puddly puddly changed the title Add a simplified async API Add a simple async API May 5, 2026
@puddly puddly merged commit 8d9bd5f into dev May 5, 2026
40 checks passed
@puddly puddly deleted the puddly/async-sync-api branch May 5, 2026 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants