From 6f552e0a3142425acdaf0329b162548f880c34f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Han?= Date: Fri, 21 Nov 2025 12:18:25 +0100 Subject: [PATCH] fix: mypy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sébastien Han --- src/llama_stack/core/inspect.py | 5 ++++- .../core/server/fastapi_router_registry.py | 16 ++++++++++------ src/llama_stack_api/router_utils.py | 4 +++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/llama_stack/core/inspect.py b/src/llama_stack/core/inspect.py index 7d8937339..0af04d4f8 100644 --- a/src/llama_stack/core/inspect.py +++ b/src/llama_stack/core/inspect.py @@ -132,7 +132,10 @@ class DistributionInspectImpl(Inspect): methods = {m for m in route.methods if m != "HEAD"} if methods and should_include_router_route(route, router_prefix): # FastAPI already combines router prefix with route path - path = route.path + # Only APIRoute has a path attribute, use getattr to safely access it + path = getattr(route, "path", None) + if path is None: + continue ret.append( RouteInfo( diff --git a/src/llama_stack/core/server/fastapi_router_registry.py b/src/llama_stack/core/server/fastapi_router_registry.py index a220ed78c..42e83b7cd 100644 --- a/src/llama_stack/core/server/fastapi_router_registry.py +++ b/src/llama_stack/core/server/fastapi_router_registry.py @@ -7,11 +7,11 @@ """Router utilities for FastAPI routers. This module provides utilities to discover and create FastAPI routers from API packages. -Routers are automatically discovered by checking for routes modules in each API package. +Routers are automatically discovered by checking for fastapi_routes modules in each API package. """ import importlib -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, cast from fastapi import APIRouter @@ -20,13 +20,13 @@ if TYPE_CHECKING: def has_router(api: "Api") -> bool: - """Check if an API has a router factory in its routes module. + """Check if an API has a router factory in its fastapi_routes module. Args: api: The API enum value Returns: - True if the API has a routes module with a create_router function + True if the API has a fastapi_routes module with a create_router function """ try: routes_module = importlib.import_module(f"llama_stack_api.{api.value}.fastapi_routes") @@ -46,13 +46,17 @@ def build_router(api: "Api", impl: Any) -> APIRouter | None: impl: The implementation instance for the API Returns: - APIRouter if the API has a routes module with create_router, None otherwise + APIRouter if the API has a fastapi_routes module with create_router, None otherwise """ try: routes_module = importlib.import_module(f"llama_stack_api.{api.value}.fastapi_routes") if hasattr(routes_module, "create_router"): router_factory = routes_module.create_router - return router_factory(impl) + # cast is safe here: mypy can't verify the return type statically because + # we're dynamically importing the module. However, all router factories in + # API packages are required to return APIRouter. If a router factory returns the wrong + # type, it will fail at runtime when app.include_router(router) is called + return cast(APIRouter, router_factory(impl)) except (ImportError, AttributeError): pass diff --git a/src/llama_stack_api/router_utils.py b/src/llama_stack_api/router_utils.py index 1ad19b05f..fd0efe060 100644 --- a/src/llama_stack_api/router_utils.py +++ b/src/llama_stack_api/router_utils.py @@ -11,7 +11,9 @@ These responses use OpenAPI $ref references to component responses defined in the OpenAPI specification. """ -standard_responses = { +from typing import Any + +standard_responses: dict[int | str, dict[str, Any]] = { 400: {"$ref": "#/components/responses/BadRequest400"}, 429: {"$ref": "#/components/responses/TooManyRequests429"}, 500: {"$ref": "#/components/responses/InternalServerError500"},