Skip to content

Conversation

@jtroup
Copy link
Contributor

@jtroup jtroup commented Nov 28, 2025

what

This PR fixes mypy type checking to work consistently in local development environments and CI:

  1. Added null check in stacklet/mcp/lifespan.py: Added an assertion to ensure ctx.request_context is not None before accessing its attributes (line 51)

  2. Added pip to dev dependencies: mypy requires pip to auto-install type stubs, but uv venvs don't include it by default

  3. Added explicit type stub packages: Added types-cachetools, types-pyyaml, and types-requests to dev dependencies for reproducible builds with pinned type versions

why

The Problem:

  • CI was failing with a mypy error: Item "None" of "RequestContext[Any, Any, Request] | None" has no attribute "lifespan_context"
  • The error only appeared in CI, not in local development

Root Cause:
When running mypy in a uv-managed venv without pip, mypy attempts to auto-install type stubs but silently fails. This causes mypy to continue without full type information, resulting in a false positive "Success" even when type errors exist.

The Fix:

  • The code fix (assertion) satisfies mypy's strict type checking by explicitly handling the nullable type
  • Adding pip ensures mypy can install additional stubs if needed (safety net)
  • Adding explicit type stub packages ensures reproducible builds with known versions and prevents the silent failure scenario

This gives us the best of both worlds: reproducible builds with pinned type stubs, plus a safety net if mypy needs to install additional stubs in the future.

testing

Tested the fix by:

  1. Reproduced the issue: Cleared pre-commit cache and confirmed the original error appeared in CI-like conditions
  2. Verified the fix: Applied changes and ran uv run pre-commit run --all-files - all checks passed
  3. Tested with and without the assertion: Confirmed that with type stubs installed, mypy correctly catches the type error when the assertion is removed

All pre-commit hooks now pass including:

  • mypy type checking
  • ruff linting and formatting
  • pyproject-fmt formatting

docs

No user-facing documentation needed. This is an internal development tooling fix.

The inline comments in pyproject.toml explain why pip and type stubs are included in dev dependencies.

Copy link
Contributor

@tvansteenburgh tvansteenburgh left a comment

Choose a reason for hiding this comment

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

Nice, thanks @jtroup

@jtroup jtroup merged commit f4d367d into main Dec 1, 2025
5 checks passed
@jtroup jtroup deleted the elmo/fix-mypy-lint branch December 1, 2025 16:52
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.

3 participants