Repository navigation
fix(execution): raise StreamError instead of assert guards in MarlinStreamer - #111
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replace the six
assert self._transport is not Noneguards inMarlinStreamerwith explicitif self._transport is None: raise StreamError(...).Why
assertis compiled out underpython -O. In an optimized build these guards vanish and the next line dereferencesNone, turning a clear failure into an opaqueAttributeErrormid-stream. Theif/raiseform fails descriptively and behaves identically underpythonandpython -O, while preserving mypy's type narrowing exactly asassertdid.These guards are unreachable in normal flow — every site is dominated by
_ensure_connection(), which guarantees a non-None transport (raisingStreamErroritself if no port/transport is available). So this is defense-in-depth with no behavior change on real paths; the value is correctness under-Oand against any future direct-call refactor.How / verification
pause,resume,emergency_stop,_wait_for_marlin_ready,send_command,_await_ok.StreamError(defined locally inmarlin.py:14, already the class's standard failure type) — no new import.StreamError(notAttributeError) forsend_command/pause/resume/emergency_stop, plus a subprocess test proving the guard still raises underpython -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