Skip to content

tests: remove lax-no-cover pragmas by moving assertions before cancellation#2206

Merged
maxisbey merged 2 commits intomainfrom
fix/sse-test-coverage-pragmas
Mar 4, 2026
Merged

tests: remove lax-no-cover pragmas by moving assertions before cancellation#2206
maxisbey merged 2 commits intomainfrom
fix/sse-test-coverage-pragmas

Conversation

@maxisbey
Copy link
Contributor

@maxisbey maxisbey commented Mar 4, 2026

Follow-up to #2200 — applies the same refactor to the 11 pre-existing pragmas of the same class.

Motivation

On Python 3.11 with coverage 7.10.7 (lowest-direct), statements immediately following an async with block whose __aexit__ processes cancellation are intermittently not traced. Both sse_client and streamable_http_client call tg.cancel_scope.cancel() on exit, so any plain statement after those blocks falls into this gap. 3.10 uses older bytecode, 3.12+ uses sys.monitoring — only 3.11+lowest-direct is affected.

#1897 and #1991 handled this by adding # pragma: lax no cover to the affected lines. That works, but it means those assertions are never verified by coverage on any platform.

Approach

Every one of these 11 lines checks state that is fully populated before cancellation fires, so they can simply move inside the async with:

State Populated by Safe because
on_session_created callback endpoint SSE event Fires before sse_client yields
Session ID / protocol version initialize() response Captured long before block exit
Notification count in resumption test message_handler The while loop only exits after append; server tool is blocked on a lock, so nothing else arrives
Resumption tokens on_resumption_token_update All fire before send_request returns

Also drops two dead if captured_session_id: branches that were always truthy (asserted not-None three lines earlier).

Changes

  • tests/shared/test_sse.py: 3 pragmas removed across 2 tests
  • tests/shared/test_streamable_http.py: 8 pragmas removed across 4 tests
  • Net −12 lines

AI Disclaimer

maxisbey added 2 commits March 4, 2026 14:48
On Python 3.11 with coverage 7.10.7 (lowest-direct), statements
immediately following an async with block whose __aexit__ processes
cancellation are intermittently not traced. Both sse_client and
streamable_http_client call tg.cancel_scope.cancel() in their exit
paths, and test_streamable_http_client_resumption has an explicit
in-test cancel, so 11 lines across 6 tests were carrying pragmas.

All of these lines check state that is fully populated before the
cancellation fires, so they can move inside the blocks:

- on_session_created fires when the endpoint SSE event arrives, before
  sse_client yields to the caller
- session IDs and protocol versions are captured during initialize(),
  so headers for phase-2 reconnects can be built inside phase 1
- the resumption test's while loop only exits after the notification
  has been appended, and the server tool blocks on a lock, so the
  count is stable before cancel
- resumption tokens are all captured before send_request returns

Also drops two dead if-truthy checks where the value was already
asserted not-None three lines earlier.
The block-exit arcs from these three async with lines were never
actually traced on 3.11 or 3.14 — on main, the arc destinations were
lax-no-cover lines, so coverage silently excluded the arcs from
counting. Removing those destinations in the previous commit exposed
the latent gap.

Adding no branch here is a smaller suppression than what we removed:
the assertions that used to be lax-no-cover are now fully verified,
and we only suppress branch-exit tracking on async with lines that
have no actual branching. Matches the existing no branch on the
phase-2 ClientSession lines in these same tests.
@maxisbey maxisbey marked this pull request as ready for review March 4, 2026 15:34
Copy link
Contributor

@felixweinberger felixweinberger left a comment

Choose a reason for hiding this comment

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

🧹

@maxisbey maxisbey merged commit 528abfa into main Mar 4, 2026
31 checks passed
@maxisbey maxisbey deleted the fix/sse-test-coverage-pragmas branch March 4, 2026 16:11
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