fix: address mypy type errors in multiple files

- Fix return type and argument handling in cache_tools.py
- Add missing return statements in agent.py
- Fix _inject_date_to_task signature to accept Task object
- Remove unused type:ignore comments in tool_usage.py
- Add type annotations to internal methods in mem0_storage.py
This commit is contained in:
Greyson LaLonde
2025-09-03 23:05:07 -04:00
parent 06d5c3f170
commit 90ca02b9dc
4 changed files with 17 additions and 10 deletions

View File

@@ -620,6 +620,7 @@ class Agent(BaseAgent):
self._logger.log(
"info", "Coding tools not available. Install crewai_tools. "
)
return []
def get_output_converter(
self, llm: BaseLLM, text: str, model: str, instructions: str
@@ -671,7 +672,7 @@ class Agent(BaseAgent):
return description
def _inject_date_to_task(self, task: str) -> str:
def _inject_date_to_task(self, task: Task) -> None:
"""Inject the current date into the task description if inject_date is enabled."""
if self.inject_date:
from datetime import datetime

View File

@@ -15,7 +15,9 @@ class Mem0Storage(Storage):
Extends Storage to handle embedding and searching across entities using Mem0.
"""
def __init__(self, type, crew=None, config=None):
def __init__(
self, type: str, crew: Any = None, config: dict[str, Any] | None = None
) -> None:
super().__init__()
self._validate_type(type)
@@ -26,21 +28,21 @@ class Mem0Storage(Storage):
self._extract_config_values()
self._initialize_memory()
def _validate_type(self, type):
def _validate_type(self, type: str) -> None:
supported_types = {"short_term", "long_term", "entities", "external"}
if type not in supported_types:
raise ValueError(
f"Invalid type '{type}' for Mem0Storage. Must be one of: {', '.join(supported_types)}"
)
def _extract_config_values(self):
def _extract_config_values(self) -> None:
self.mem0_run_id = self.config.get("run_id")
self.includes = self.config.get("includes")
self.excludes = self.config.get("excludes")
self.custom_categories = self.config.get("custom_categories")
self.infer = self.config.get("infer", True)
def _initialize_memory(self):
def _initialize_memory(self) -> None:
api_key = self.config.get("api_key") or os.getenv("MEM0_API_KEY")
org_id = self.config.get("org_id")
project_id = self.config.get("project_id")
@@ -61,7 +63,7 @@ class Mem0Storage(Storage):
else Memory()
)
def _create_filter_for_search(self):
def _create_filter_for_search(self) -> dict[str, Any]:
"""
Returns:
dict: A filter dictionary containing AND conditions for querying data.

View File

@@ -21,7 +21,11 @@ class CacheTools(BaseModel):
)
def hit_cache(self, key: str) -> str:
import json
split = key.split("tool:")
tool = split[1].split("|input:")[0].strip()
tool_input = split[1].split("|input:")[1].strip()
return self.cache_handler.read(tool, tool_input)
tool_input_str = split[1].split("|input:")[1].strip()
tool_input = json.loads(tool_input_str) if tool_input_str else None
result = self.cache_handler.read(tool, tool_input)
return result if result is not None else ""

View File

@@ -204,7 +204,7 @@ class ToolUsage:
if self.tools_handler and self.tools_handler.cache:
result = self.tools_handler.cache.read(
tool=calling.tool_name, input_data=calling.arguments
) # type: ignore
)
from_cache = result is not None
available_tool = next(
@@ -322,7 +322,7 @@ class ToolUsage:
and available_tool.result_as_answer # type: ignore # Item "None" of "Any | None" has no attribute "cache_function"
):
result_as_answer = available_tool.result_as_answer # type: ignore # Item "None" of "Any | None" has no attribute "result_as_answer"
data["result_as_answer"] = result_as_answer # type: ignore
data["result_as_answer"] = result_as_answer
if self.agent and hasattr(self.agent, "tools_results"):
self.agent.tools_results.append(data)