mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-12-03 18:00:36 +00:00
fix: mypy
Signed-off-by: Sébastien Han <seb@redhat.com>
This commit is contained in:
parent
234eaf4709
commit
6f552e0a31
3 changed files with 17 additions and 8 deletions
|
|
@ -132,7 +132,10 @@ class DistributionInspectImpl(Inspect):
|
||||||
methods = {m for m in route.methods if m != "HEAD"}
|
methods = {m for m in route.methods if m != "HEAD"}
|
||||||
if methods and should_include_router_route(route, router_prefix):
|
if methods and should_include_router_route(route, router_prefix):
|
||||||
# FastAPI already combines router prefix with route path
|
# 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(
|
ret.append(
|
||||||
RouteInfo(
|
RouteInfo(
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,11 @@
|
||||||
"""Router utilities for FastAPI routers.
|
"""Router utilities for FastAPI routers.
|
||||||
|
|
||||||
This module provides utilities to discover and create FastAPI routers from API packages.
|
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
|
import importlib
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any, cast
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
|
||||||
|
|
@ -20,13 +20,13 @@ if TYPE_CHECKING:
|
||||||
|
|
||||||
|
|
||||||
def has_router(api: "Api") -> bool:
|
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:
|
Args:
|
||||||
api: The API enum value
|
api: The API enum value
|
||||||
|
|
||||||
Returns:
|
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:
|
try:
|
||||||
routes_module = importlib.import_module(f"llama_stack_api.{api.value}.fastapi_routes")
|
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
|
impl: The implementation instance for the API
|
||||||
|
|
||||||
Returns:
|
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:
|
try:
|
||||||
routes_module = importlib.import_module(f"llama_stack_api.{api.value}.fastapi_routes")
|
routes_module = importlib.import_module(f"llama_stack_api.{api.value}.fastapi_routes")
|
||||||
if hasattr(routes_module, "create_router"):
|
if hasattr(routes_module, "create_router"):
|
||||||
router_factory = 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):
|
except (ImportError, AttributeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,9 @@ These responses use OpenAPI $ref references to component responses defined
|
||||||
in the OpenAPI specification.
|
in the OpenAPI specification.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
standard_responses = {
|
from typing import Any
|
||||||
|
|
||||||
|
standard_responses: dict[int | str, dict[str, Any]] = {
|
||||||
400: {"$ref": "#/components/responses/BadRequest400"},
|
400: {"$ref": "#/components/responses/BadRequest400"},
|
||||||
429: {"$ref": "#/components/responses/TooManyRequests429"},
|
429: {"$ref": "#/components/responses/TooManyRequests429"},
|
||||||
500: {"$ref": "#/components/responses/InternalServerError500"},
|
500: {"$ref": "#/components/responses/InternalServerError500"},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue