fix: mypy

Signed-off-by: Sébastien Han <seb@redhat.com>
This commit is contained in:
Sébastien Han 2025-11-21 12:18:25 +01:00
parent 234eaf4709
commit 6f552e0a31
No known key found for this signature in database
3 changed files with 17 additions and 8 deletions

View file

@ -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(

View file

@ -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

View file

@ -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"},