From c7b7492876be69f3dc60b0a5d9671ea822ff8ab0 Mon Sep 17 00:00:00 2001 From: Greyson LaLonde Date: Fri, 19 Sep 2025 14:23:01 -0400 Subject: [PATCH] chore: fix ruff linting issues in project module --- .../storage/kickoff_task_outputs_storage.py | 22 +++++++++---------- .../memory/storage/ltm_sqlite_storage.py | 18 ++++++--------- src/crewai/project/__init__.py | 14 ++++++------ src/crewai/project/annotations.py | 16 ++++++-------- 4 files changed, 32 insertions(+), 38 deletions(-) diff --git a/src/crewai/memory/storage/kickoff_task_outputs_storage.py b/src/crewai/memory/storage/kickoff_task_outputs_storage.py index c84c54f1c..c8643a153 100644 --- a/src/crewai/memory/storage/kickoff_task_outputs_storage.py +++ b/src/crewai/memory/storage/kickoff_task_outputs_storage.py @@ -2,7 +2,7 @@ import json import logging import sqlite3 from pathlib import Path -from typing import Any, Dict, List, Optional +from typing import Any from crewai.task import Task from crewai.utilities import Printer @@ -18,7 +18,7 @@ class KickoffTaskOutputsSQLiteStorage: An updated SQLite storage class for kickoff task outputs storage. """ - def __init__(self, db_path: Optional[str] = None) -> None: + def __init__(self, db_path: str | None = None) -> None: if db_path is None: # Get the parent directory of the default db path and create our db file there db_path = str(Path(db_storage_path()) / "latest_kickoff_task_outputs.db") @@ -57,15 +57,15 @@ class KickoffTaskOutputsSQLiteStorage: except sqlite3.Error as e: error_msg = DatabaseError.format_error(DatabaseError.INIT_ERROR, e) logger.error(error_msg) - raise DatabaseOperationError(error_msg, e) + raise DatabaseOperationError(error_msg, e) from e def add( self, task: Task, - output: Dict[str, Any], + output: dict[str, Any], task_index: int, was_replayed: bool = False, - inputs: Dict[str, Any] | None = None, + inputs: dict[str, Any] | None = None, ) -> None: """Add a new task output record to the database. @@ -103,7 +103,7 @@ class KickoffTaskOutputsSQLiteStorage: except sqlite3.Error as e: error_msg = DatabaseError.format_error(DatabaseError.SAVE_ERROR, e) logger.error(error_msg) - raise DatabaseOperationError(error_msg, e) + raise DatabaseOperationError(error_msg, e) from e def update( self, @@ -138,7 +138,7 @@ class KickoffTaskOutputsSQLiteStorage: else value ) - query = f"UPDATE latest_kickoff_task_outputs SET {', '.join(fields)} WHERE task_index = ?" # nosec + query = f"UPDATE latest_kickoff_task_outputs SET {', '.join(fields)} WHERE task_index = ?" # nosec # noqa: S608 values.append(task_index) cursor.execute(query, tuple(values)) @@ -151,9 +151,9 @@ class KickoffTaskOutputsSQLiteStorage: except sqlite3.Error as e: error_msg = DatabaseError.format_error(DatabaseError.UPDATE_ERROR, e) logger.error(error_msg) - raise DatabaseOperationError(error_msg, e) + raise DatabaseOperationError(error_msg, e) from e - def load(self) -> List[Dict[str, Any]]: + def load(self) -> list[dict[str, Any]]: """Load all task output records from the database. Returns: @@ -192,7 +192,7 @@ class KickoffTaskOutputsSQLiteStorage: except sqlite3.Error as e: error_msg = DatabaseError.format_error(DatabaseError.LOAD_ERROR, e) logger.error(error_msg) - raise DatabaseOperationError(error_msg, e) + raise DatabaseOperationError(error_msg, e) from e def delete_all(self) -> None: """Delete all task output records from the database. @@ -212,4 +212,4 @@ class KickoffTaskOutputsSQLiteStorage: except sqlite3.Error as e: error_msg = DatabaseError.format_error(DatabaseError.DELETE_ERROR, e) logger.error(error_msg) - raise DatabaseOperationError(error_msg, e) + raise DatabaseOperationError(error_msg, e) from e diff --git a/src/crewai/memory/storage/ltm_sqlite_storage.py b/src/crewai/memory/storage/ltm_sqlite_storage.py index 35f54e0e7..abf117c63 100644 --- a/src/crewai/memory/storage/ltm_sqlite_storage.py +++ b/src/crewai/memory/storage/ltm_sqlite_storage.py @@ -1,7 +1,7 @@ import json import sqlite3 from pathlib import Path -from typing import Any, Dict, List, Optional, Union +from typing import Any from crewai.utilities import Printer from crewai.utilities.paths import db_storage_path @@ -12,9 +12,7 @@ class LTMSQLiteStorage: An updated SQLite storage class for LTM data storage. """ - def __init__( - self, db_path: Optional[str] = None - ) -> None: + def __init__(self, db_path: str | None = None) -> None: if db_path is None: # Get the parent directory of the default db path and create our db file there db_path = str(Path(db_storage_path()) / "long_term_memory_storage.db") @@ -53,9 +51,9 @@ class LTMSQLiteStorage: def save( self, task_description: str, - metadata: Dict[str, Any], + metadata: dict[str, Any], datetime: str, - score: Union[int, float], + score: int | float, ) -> None: """Saves data to the LTM table with error handling.""" try: @@ -75,9 +73,7 @@ class LTMSQLiteStorage: color="red", ) - def load( - self, task_description: str, latest_n: int - ) -> Optional[List[Dict[str, Any]]]: + def load(self, task_description: str, latest_n: int) -> list[dict[str, Any]] | None: """Queries the LTM table by task description with error handling.""" try: with sqlite3.connect(self.db_path) as conn: @@ -89,7 +85,7 @@ class LTMSQLiteStorage: WHERE task_description = ? ORDER BY datetime DESC, score ASC LIMIT {latest_n} - """, # nosec + """, # nosec # noqa: S608 (task_description,), ) rows = cursor.fetchall() @@ -125,4 +121,4 @@ class LTMSQLiteStorage: content=f"MEMORY ERROR: An error occurred while deleting all rows in LTM: {e}", color="red", ) - return None + return diff --git a/src/crewai/project/__init__.py b/src/crewai/project/__init__.py index d60212153..7aabbebe1 100644 --- a/src/crewai/project/__init__.py +++ b/src/crewai/project/__init__.py @@ -14,16 +14,16 @@ from .annotations import ( from .crew_base import CrewBase __all__ = [ + "CrewBase", + "after_kickoff", "agent", + "before_kickoff", + "cache_handler", + "callback", "crew", - "task", + "llm", "output_json", "output_pydantic", + "task", "tool", - "callback", - "CrewBase", - "llm", - "cache_handler", - "before_kickoff", - "after_kickoff", ] diff --git a/src/crewai/project/annotations.py b/src/crewai/project/annotations.py index d7c636ccf..d14f57b37 100644 --- a/src/crewai/project/annotations.py +++ b/src/crewai/project/annotations.py @@ -1,5 +1,5 @@ +from collections.abc import Callable from functools import wraps -from typing import Callable from crewai import Crew from crewai.project.utils import memoize @@ -36,15 +36,13 @@ def task(func): def agent(func): """Marks a method as a crew agent.""" func.is_agent = True - func = memoize(func) - return func + return memoize(func) def llm(func): """Marks a method as an LLM provider.""" func.is_llm = True - func = memoize(func) - return func + return memoize(func) def output_json(cls): @@ -91,7 +89,7 @@ def crew(func) -> Callable[..., Crew]: agents = self._original_agents.items() # Instantiate tasks in order - for task_name, task_method in tasks: + for _task_name, task_method in tasks: task_instance = task_method(self) instantiated_tasks.append(task_instance) agent_instance = getattr(task_instance, "agent", None) @@ -100,7 +98,7 @@ def crew(func) -> Callable[..., Crew]: agent_roles.add(agent_instance.role) # Instantiate agents not included by tasks - for agent_name, agent_method in agents: + for _agent_name, agent_method in agents: agent_instance = agent_method(self) if agent_instance.role not in agent_roles: instantiated_agents.append(agent_instance) @@ -117,9 +115,9 @@ def crew(func) -> Callable[..., Crew]: return wrapper - for _, callback in self._before_kickoff.items(): + for callback in self._before_kickoff.values(): crew.before_kickoff_callbacks.append(callback_wrapper(callback, self)) - for _, callback in self._after_kickoff.items(): + for callback in self._after_kickoff.values(): crew.after_kickoff_callbacks.append(callback_wrapper(callback, self)) return crew