Fix linter issues: specify exception type and use type parameters

This commit is contained in:
Ashwin Bharambe 2025-10-05 18:13:09 -07:00
parent 2d87a055e1
commit 6be4cf7d18
7 changed files with 18 additions and 54 deletions

View file

@ -23,7 +23,6 @@ from llama_stack.apis.conversations.conversations import (
)
from llama_stack.core.datatypes import AccessRule, StackRunConfig
from llama_stack.core.persistence_resolver import resolve_conversations_store_config
from llama_stack.core.utils.config_dirs import DISTRIBS_BASE_DIR
from llama_stack.log import get_logger
from llama_stack.providers.utils.sqlstore.api import ColumnDefinition, ColumnType
from llama_stack.providers.utils.sqlstore.authorized_sqlstore import AuthorizedSqlStore

View file

@ -28,7 +28,6 @@ from llama_stack.apis.vector_io import VectorIO
from llama_stack.core.access_control.datatypes import AccessRule
from llama_stack.providers.datatypes import Api, ProviderSpec
from llama_stack.providers.utils.kvstore.config import (
KVStoreConfig,
MongoDBKVStoreConfig,
PostgresKVStoreConfig,
RedisKVStoreConfig,
@ -37,7 +36,6 @@ from llama_stack.providers.utils.kvstore.config import (
from llama_stack.providers.utils.sqlstore.sqlstore import (
PostgresSqlStoreConfig,
SqliteSqlStoreConfig,
SqlStoreConfig,
)
LLAMA_STACK_BUILD_CONFIG_VERSION = 2
@ -518,7 +516,7 @@ class PersistenceConfig(BaseModel):
# Unknown usage - try SqlStore first (for backward compat), then KVStore
try:
parsed_backends[name] = _parse_sqlstore_config(config)
except:
except ValueError:
parsed_backends[name] = _parse_kvstore_config(config)
data["backends"] = parsed_backends
@ -544,12 +542,9 @@ class PersistenceConfig(BaseModel):
return self
def _parse_kvstore_config(config: dict) -> (
RedisKVStoreConfig
| SqliteKVStoreConfig
| PostgresKVStoreConfig
| MongoDBKVStoreConfig
):
def _parse_kvstore_config(
config: dict,
) -> RedisKVStoreConfig | SqliteKVStoreConfig | PostgresKVStoreConfig | MongoDBKVStoreConfig:
"""Parse a KVStore config from dict."""
type_val = config.get("type")
if type_val == "redis":

View file

@ -4,10 +4,9 @@
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.
from typing import Callable, TypeVar
from collections.abc import Callable
from llama_stack.core.datatypes import (
InferenceStoreReference,
PersistenceConfig,
StoreReference,
)
@ -32,10 +31,8 @@ KVStoreConfigTypes = (
)
SqlStoreConfigTypes = (SqliteSqlStoreConfig, PostgresSqlStoreConfig)
T = TypeVar("T")
def resolve_backend(
def resolve_backend[T](
persistence: PersistenceConfig | None,
store_ref: StoreReference | None,
default_factory: Callable[[], T],
@ -59,8 +56,7 @@ def resolve_backend(
backend_config = persistence.backends.get(store_ref.backend)
if not backend_config:
raise ValueError(
f"Backend '{store_ref.backend}' referenced by store '{store_name}' "
f"not found in persistence.backends"
f"Backend '{store_ref.backend}' referenced by store '{store_name}' not found in persistence.backends"
)
# Clone backend and apply namespace if KVStore
@ -96,14 +92,11 @@ def resolve_inference_store_config(
backend_config = persistence.backends.get(inference_ref.backend)
if not backend_config:
raise ValueError(
f"Backend '{inference_ref.backend}' referenced by inference store "
f"not found in persistence.backends"
f"Backend '{inference_ref.backend}' referenced by inference store not found in persistence.backends"
)
if not isinstance(backend_config, SqlStoreConfigTypes):
raise ValueError(
f"Inference store requires SqlStore backend, got {type(backend_config).__name__}"
)
raise ValueError(f"Inference store requires SqlStore backend, got {type(backend_config).__name__}")
return (
backend_config, # type: ignore
@ -115,12 +108,7 @@ def resolve_inference_store_config(
def resolve_metadata_store_config(
persistence: PersistenceConfig | None,
image_name: str,
) -> (
RedisKVStoreConfig
| SqliteKVStoreConfig
| PostgresKVStoreConfig
| MongoDBKVStoreConfig
):
) -> RedisKVStoreConfig | SqliteKVStoreConfig | PostgresKVStoreConfig | MongoDBKVStoreConfig:
"""
Resolve metadata store configuration.
@ -131,9 +119,7 @@ def resolve_metadata_store_config(
Returns:
Resolved KVStore config
"""
default_config = SqliteKVStoreConfig(
db_path=(DISTRIBS_BASE_DIR / image_name / "kvstore.db").as_posix()
)
default_config = SqliteKVStoreConfig(db_path=(DISTRIBS_BASE_DIR / image_name / "kvstore.db").as_posix())
if not persistence or not persistence.stores or not persistence.stores.metadata:
return default_config
@ -142,14 +128,11 @@ def resolve_metadata_store_config(
backend_config = persistence.backends.get(metadata_ref.backend)
if not backend_config:
raise ValueError(
f"Backend '{metadata_ref.backend}' referenced by metadata store "
f"not found in persistence.backends"
f"Backend '{metadata_ref.backend}' referenced by metadata store not found in persistence.backends"
)
if not isinstance(backend_config, KVStoreConfigTypes):
raise ValueError(
f"Metadata store requires KVStore backend, got {type(backend_config).__name__}"
)
raise ValueError(f"Metadata store requires KVStore backend, got {type(backend_config).__name__}")
# Apply namespace if specified
config_dict = backend_config.model_dump()
@ -167,9 +150,7 @@ def resolve_conversations_store_config(
Returns:
Resolved SqlStore config
"""
default_config = SqliteSqlStoreConfig(
db_path=(RUNTIME_BASE_DIR / "conversations.db").as_posix()
)
default_config = SqliteSqlStoreConfig(db_path=(RUNTIME_BASE_DIR / "conversations.db").as_posix())
if not persistence or not persistence.stores or not persistence.stores.conversations:
return default_config
@ -178,13 +159,10 @@ def resolve_conversations_store_config(
backend_config = persistence.backends.get(conversations_ref.backend)
if not backend_config:
raise ValueError(
f"Backend '{conversations_ref.backend}' referenced by conversations store "
f"not found in persistence.backends"
f"Backend '{conversations_ref.backend}' referenced by conversations store not found in persistence.backends"
)
if not isinstance(backend_config, SqlStoreConfigTypes):
raise ValueError(
f"Conversations store requires SqlStore backend, got {type(backend_config).__name__}"
)
raise ValueError(f"Conversations store requires SqlStore backend, got {type(backend_config).__name__}")
return backend_config # type: ignore

View file

@ -12,9 +12,7 @@ from pydantic import BaseModel
from llama_stack.apis.prompts import ListPromptsResponse, Prompt, Prompts
from llama_stack.core.datatypes import StackRunConfig
from llama_stack.core.persistence_resolver import resolve_metadata_store_config
from llama_stack.core.utils.config_dirs import DISTRIBS_BASE_DIR
from llama_stack.providers.utils.kvstore import KVStore, kvstore_impl
from llama_stack.providers.utils.kvstore.config import SqliteKVStoreConfig
class PromptServiceConfig(BaseModel):

View file

@ -83,9 +83,7 @@ async def get_auto_router_impl(
# TODO: move pass configs to routers instead
if api == Api.inference:
sql_config, max_queue, num_writers = resolve_inference_store_config(
run_config.persistence
)
sql_config, max_queue, num_writers = resolve_inference_store_config(run_config.persistence)
inference_store_config = InferenceStoreConfig(
sql_store_config=sql_config,
max_write_queue_size=max_queue,

View file

@ -12,10 +12,8 @@ import pydantic
from llama_stack.core.datatypes import PersistenceConfig, RoutableObjectWithProvider
from llama_stack.core.persistence_resolver import resolve_metadata_store_config
from llama_stack.core.utils.config_dirs import DISTRIBS_BASE_DIR
from llama_stack.log import get_logger
from llama_stack.providers.utils.kvstore import KVStore, kvstore_impl
from llama_stack.providers.utils.kvstore.config import KVStoreConfig, SqliteKVStoreConfig
logger = get_logger(__name__, category="core::registry")

View file

@ -71,9 +71,7 @@ def test_mixed_backend_types_allowed():
config = PersistenceConfig(
backends={
"kvstore": SqliteKVStoreConfig(db_path="/tmp/kv.db"),
"sqlstore": PostgresSqlStoreConfig(
user="test", password="test", host="localhost", db="test"
),
"sqlstore": PostgresSqlStoreConfig(user="test", password="test", host="localhost", db="test"),
},
stores=StoresConfig(
metadata=StoreReference(backend="kvstore"),