From 41a65b87e6ec01ca1d35a265dac270231d46de0a Mon Sep 17 00:00:00 2001 From: Ashwin Bharambe Date: Mon, 27 Oct 2025 21:09:44 -0700 Subject: [PATCH] fix(mypy): resolve SQLAlchemy typing issues in sqlalchemy_sqlstore.py Fix all 7 mypy type checking errors in SQLAlchemy store implementation without using suppressions. **Changes:** - Add `cast()` import for type assertions - Use `cast(ColumnElement[Any], ...)` for all comparison operators in `_build_where_expr()` - Handles ==, >, <, >=, <= operators - Remove unnecessary `rowcount` check before iteration - SQLAlchemy Result can be iterated directly - Empty results yield empty list naturally **Errors fixed:** - 6 `no-any-return` errors in _build_where_expr (comparison operators) - 1 `attr-defined` error (Result.rowcount) **Testing:** ```bash uv run mypy src/llama_stack/providers/utils/sqlstore/sqlalchemy_sqlstore.py # Success: no issues found ``` --- .../utils/sqlstore/sqlalchemy_sqlstore.py | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/llama_stack/providers/utils/sqlstore/sqlalchemy_sqlstore.py b/src/llama_stack/providers/utils/sqlstore/sqlalchemy_sqlstore.py index c1ccd73dd..1bd364d43 100644 --- a/src/llama_stack/providers/utils/sqlstore/sqlalchemy_sqlstore.py +++ b/src/llama_stack/providers/utils/sqlstore/sqlalchemy_sqlstore.py @@ -4,7 +4,7 @@ # This source code is licensed under the terms described in the LICENSE file in # the root directory of this source tree. from collections.abc import Mapping, Sequence -from typing import Any, Literal +from typing import Any, Literal, cast from sqlalchemy import ( JSON, @@ -55,17 +55,17 @@ def _build_where_expr(column: ColumnElement, value: Any) -> ColumnElement: raise ValueError(f"Operator mapping must have a single operator, got: {value}") op, operand = next(iter(value.items())) if op == "==" or op == "=": - return column == operand + return cast(ColumnElement[Any], column == operand) if op == ">": - return column > operand + return cast(ColumnElement[Any], column > operand) if op == "<": - return column < operand + return cast(ColumnElement[Any], column < operand) if op == ">=": - return column >= operand + return cast(ColumnElement[Any], column >= operand) if op == "<=": - return column <= operand + return cast(ColumnElement[Any], column <= operand) raise ValueError(f"Unsupported operator '{op}' in where mapping") - return column == value + return cast(ColumnElement[Any], column == value) class SqlAlchemySqlStoreImpl(SqlStore): @@ -210,10 +210,8 @@ class SqlAlchemySqlStoreImpl(SqlStore): query = query.limit(fetch_limit) result = await session.execute(query) - if result.rowcount == 0: - rows = [] - else: - rows = [dict(row._mapping) for row in result] + # Iterate directly - if no rows, list comprehension yields empty list + rows = [dict(row._mapping) for row in result] # Always return pagination result has_more = False