Skip to content

fix(execution): raise StreamError instead of assert guards in MarlinStreamer - #111

Merged
CameronBrooks11 merged 1 commit into
mainfrom
fix/marlin-streamerror-guards
Jun 24, 2026
Merged

CameronBrooks11 merged 1 commit into
mainfrom
fix/marlin-streamerror-guards

Conversation

@CameronBrooks11

Copy link
Copy Markdown
Member

What

Replace the six assert self._transport is not None guards in MarlinStreamer with explicit if self._transport is None: raise StreamError(...).

Why

assert is compiled out under python -O. In an optimized build these guards vanish and the next line dereferences None, turning a clear failure into an opaque AttributeError mid-stream. The if/raise form fails descriptively and behaves identically under python and python -O, while preserving mypy's type narrowing exactly as assert did.

These guards are unreachable in normal flow — every site is dominated by _ensure_connection(), which guarantees a non-None transport (raising StreamError itself if no port/transport is available). So this is defense-in-depth with no behavior change on real paths; the value is correctness under -O and against any future direct-call refactor.

How / verification

  • Sites converted: pause, resume, emergency_stop, _wait_for_marlin_ready, send_command, _await_ok.
  • Exception type: StreamError (defined locally in marlin.py:14, already the class's standard failure type) — no new import.
  • New tests: parametrized guard test asserting StreamError (not AttributeError) for send_command/pause/resume/emergency_stop, plus a subprocess test proving the guard still raises under python -O.
  • ruff, ruff format --check, mypy (strict), pytest tests/execution: all green (11 passed).

Note: the [DEBUG] prints in these same methods are addressed separately in #102, which will rebase on top of this PR.

Closes #103

…treamer

The six `assert self._transport is not None` guards are stripped under
`python -O`, turning a guarded state into a None dereference
(AttributeError). Replace each with an explicit `if ... is None: raise
StreamError(...)` so the failure is descriptive and identical under both
`python` and `python -O`. The `if/raise` form preserves mypy's type
narrowing exactly as `assert` did.

These guards are unreachable in normal flow (every one is dominated by
_ensure_connection, which guarantees a non-None transport), so this is
defense-in-depth with no behavior change on real paths.

Adds tests covering the guard path (StreamError, not AttributeError) and a
subprocess test proving identical behavior under `python -O`.

Closes #103
@CameronBrooks11 CameronBrooks11 added the python Pull requests that update Python code label Jun 24, 2026
@CameronBrooks11
CameronBrooks11 merged commit 95535e3 into main Jun 24, 2026
24 checks passed
@CameronBrooks11
CameronBrooks11 deleted the fix/marlin-streamerror-guards branch June 24, 2026 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace assert guards with StreamError in MarlinStreamer

1 participant