From 137f8f46d7cbacc02e7c512e03ef7fc138729231 Mon Sep 17 00:00:00 2001 From: rzsultan-boop Date: Fri, 24 Apr 2026 19:23:17 +0300 Subject: [PATCH] Add isinstance guard when restoring stream attrs in include_router self.routes[-1] is typed as Starlette's BaseRoute, which lacks the stream_item_type/stream_item_field/app attributes used by APIRoute. Narrow the type with an isinstance check so mypy is satisfied and the access is defensively guarded even though the preceding isinstance(route, APIRoute) plus route_class_override=type(route) make it true in practice. --- fastapi/routing.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fastapi/routing.py b/fastapi/routing.py index b773675876..8f1df321fe 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -1802,9 +1802,10 @@ class APIRouter(routing.Router): # must restore both and re-bake the handler. if route.stream_item_type is not None: new_route = self.routes[-1] - new_route.stream_item_type = route.stream_item_type - new_route.stream_item_field = route.stream_item_field - new_route.app = request_response(new_route.get_route_handler()) + if isinstance(new_route, APIRoute): + new_route.stream_item_type = route.stream_item_type + new_route.stream_item_field = route.stream_item_field + new_route.app = request_response(new_route.get_route_handler()) elif isinstance(route, routing.Route): methods = list(route.methods or []) self.add_route(