fix(fastapi): handle FastAPI 0.137 _IncludedRouter route type#122
Merged
Conversation
FastAPI 0.137.0 introduced the internal `_IncludedRouter` route type (a `BaseRoute` subclass with no `.path`) into `app.router.routes`, which broke the offline-docs helper that read `route.path` on every route via an unchecked `typing.cast(Route, route)`. Filter the docs routes by matching only real `Route` instances, leaving `_IncludedRouter` and other route types untouched (works on old and new FastAPI alike). Drop the now-unused `import typing`. `prometheus-fastapi-instrumentator` (<=8.0.0) has the same unguarded `route.path` access and is not yet fixed upstream, so cap `fastapi<0.137` in the `fastapi-metrics` extra until a fixed instrumentator ships. Upstream issue: trallnag/prometheus-fastapi-instrumentator#370 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
908c14f to
fcfcaf0
Compare
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.
Problem
CI on
mainwent red across all Python versions.just installrunsuv lock --upgrade, which pulled FastAPI 0.137.0. That release introduced an internal_IncludedRouterroute type (aBaseRoutesubclass with no.path) intoapp.router.routes, and two places readroute.pathunconditionally:lite_bootstrap/helpers/fastapi_helpers.pydidtyping.cast(Route, route).path. The cast is a runtime no-op, so it raisedAttributeError: '_IncludedRouter' object has no attribute 'path'.prometheus-fastapi-instrumentator8.0.0 (latest, third-party) — same unguardedroute.pathaccess. No upstream fix released yet.Changes
fastapi_helpers.py— filter the offline-docs routes by matching only realRouteinstances (isinstance(route, Route) and route.path in replaced_urls), leaving_IncludedRouterand other route types untouched. Correct on both old and new FastAPI, so nothing to revert when the cap lifts. Dropped the now-unusedimport typing.pyproject.toml— addedfastapi<0.137to the basefastapiextra (the single source every otherfastapi-*extra composes from), so the whole FastAPI surface resolves to a version tested end-to-end and there's no version skew between extras. Comment links the upstream issue.The cap is temporary, tied to upstream trallnag/prometheus-fastapi-instrumentator#370; lift it once a fixed instrumentator ships.
Verification
just test→ 194 passed, 100% coveragejust lint-ci→ eof-fixer, ruff format, ruff check, ty all clean🤖 Generated with Claude Code