Skip to content

ci(mypy): Enable possibly-undefined error code - #8290

Draft
sentry-junior[bot] wants to merge 2 commits into
masterfrom
ci/mypy-possibly-undefined
Draft

sentry-junior[bot] wants to merge 2 commits into
masterfrom
ci/mypy-possibly-undefined

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Description

Turns on mypy's possibly-undefined error code. It flags names that are only assigned on some code paths (in one if branch, inside a try, in a conditional import) and then used where they might not be bound. At runtime that's an UnboundLocalError, which in the SDK would crash the host app's code path.

It reported 15 errors in 11 files. Nearly all were correct code that mypy couldn't prove was safe. They're fixed without changing behavior:

  • Initialize before branching: name = None in the Litestar/Starlite route handler patch (falls through to the existing _DEFAULT_TRANSACTION_NAME fallback), and moved agent_name = None out of the try in langchain._get_request_data.
  • Restructure so the binding is obvious:
    • transport.py: return PoolManager straight from the SOCKS except ImportError instead of tracking a use_socks_proxy flag.
    • spotlight.py: compute spotlight_js_url before the try. urljoin doesn't raise URLError, so the exception handling stays the same.
    • chalice.py: pick the patched class once and use it for both reading and patching _get_view_function_response, instead of checking the version twice.
    • grpc/aio/server.py: the last of the four exhaustive request_streaming/response_streaming branches is now else.
    • stdlib.py: set_on_span starts as None and the tunnel block checks it instead of span. They're set together, and spans don't define __bool__, so the two checks are equivalent.
  • Fix an annotation: beam.raise_exception always re-raises, so it now returns NoReturn instead of None.

Latent bugs, fixed minimally (I don't know of a real trigger for either):

  • mcp.py: _tool_handler_wrapper and _prompt_handler_wrapper leave handler_name unbound if they get no positional args and no truthy name kwarg. That raises UnboundLocalError before the user's handler runs. Both now default to "unknown", the same as _resource_handler_wrapper already does.
  • quart.py: _request_websocket_started leaves request_websocket unbound if neither a request nor a websocket context is active. That shouldn't happen for request_started/websocket_started signals. It now returns early in that case. The branches are reordered so the websocket context still wins when both are active, as before.

enable_error_code now lists both possibly-undefined and unused-awaitable (from #8281, merged into this branch). The sibling mypy follow-up PRs will still conflict trivially on that line; merge the lists when rebasing.

Follow-up from #8281.

via @sentrivana.

--

View Session [Sentry]

Turn on mypy's possibly-undefined check and fix the existing errors it
reports, mostly by initializing variables or restructuring branches so
mypy can see they are always bound.

Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 139266 passed | ⏭️ 7853 skipped | Total: 147119 | Pass Rate: 94.66% | Execution Time: 447m 45s

📊 Comparison with Base Branch

Metric Change
Total Tests —
Passed Tests —
Failed Tests —
Skipped Tests —

✨ Test counts unchanged from base.

All tests are passing successfully.

✅ Patch coverage is 88.24% (15 of 17 changed executable lines covered; target 80%).
Project statement coverage is 90.21% (unchanged from base (4c1a136) to head (22ef02f)).

Changed files with executable lines (10)
File Patch coverage Changed executable lines
sentry_sdk/integrations/quart.py 66.67% 2/3 covered; missed: 185; partial branches: 182
sentry_sdk/integrations/stdlib.py 100.00% 2/2 covered; partial branches: 365
sentry_sdk/spotlight.py 0.00% 0/1 covered; missed: 137
sentry_sdk/integrations/beam.py 100.00% 1/1 covered
sentry_sdk/integrations/chalice.py 100.00% 4/4 covered
sentry_sdk/integrations/langchain.py 100.00% 1/1 covered
sentry_sdk/integrations/litestar.py 100.00% 1/1 covered
sentry_sdk/integrations/mcp.py 100.00% 2/2 covered
sentry_sdk/integrations/starlite.py 100.00% 1/1 covered
sentry_sdk/transport.py 100.00% 1/1 covered
Coverage diff
@@            Coverage Diff             @@
##        master     #8290       +/-##
==========================================
  Coverage    90.21%    90.21%        —%
==========================================
  Files          203       203         —
  Tracked lines     27335     27336        +1
  Branches     10246     10240        -6
==========================================
+ Hits         24660     24661        +1
  Misses        2675      2675         —
+ Partials      1559      1557        -2

Generated by Coverage Action

This branch has not been deployed

No deployments
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.

1 participant