Skip to content

Commit cb0efd0

Browse files
chore: fix ruff linting issues in tools module
linting, args_schema default, and validator check
1 parent db5f565 commit cb0efd0

File tree

8 files changed

+54
-50
lines changed

8 files changed

+54
-50
lines changed

src/crewai/tools/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from .base_tool import BaseTool, tool, EnvVar
1+
from .base_tool import BaseTool, EnvVar, tool
22

33
__all__ = [
44
"BaseTool",
5-
"tool",
65
"EnvVar",
7-
]
6+
"tool",
7+
]

src/crewai/tools/agent_tools/add_image_tool.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Dict, Optional, Union
2-
31
from pydantic import BaseModel, Field
42

53
from crewai.tools.base_tool import BaseTool
@@ -10,22 +8,24 @@
108

119
class AddImageToolSchema(BaseModel):
1210
image_url: str = Field(..., description="The URL or path of the image to add")
13-
action: Optional[str] = Field(
11+
action: str | None = Field(
1412
default=None, description="Optional context or question about the image"
1513
)
1614

1715

1816
class AddImageTool(BaseTool):
1917
"""Tool for adding images to the content"""
2018

21-
name: str = Field(default_factory=lambda: i18n.tools("add_image")["name"]) # type: ignore
22-
description: str = Field(default_factory=lambda: i18n.tools("add_image")["description"]) # type: ignore
19+
name: str = Field(default_factory=lambda: i18n.tools("add_image")["name"]) # type: ignore[index]
20+
description: str = Field(
21+
default_factory=lambda: i18n.tools("add_image")["description"] # type: ignore[index]
22+
)
2323
args_schema: type[BaseModel] = AddImageToolSchema
2424

2525
def _run(
2626
self,
2727
image_url: str,
28-
action: Optional[str] = None,
28+
action: str | None = None,
2929
**kwargs,
3030
) -> dict:
3131
action = action or i18n.tools("add_image")["default_action"] # type: ignore

src/crewai/tools/agent_tools/agent_tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
class AgentTools:
1010
"""Manager class for agent-related tools"""
1111

12-
def __init__(self, agents: list[BaseAgent], i18n: I18N = I18N()):
12+
def __init__(self, agents: list[BaseAgent], i18n: I18N | None = None):
1313
self.agents = agents
14-
self.i18n = i18n
14+
self.i18n = i18n if i18n is not None else I18N()
1515

1616
def tools(self) -> list[BaseTool]:
1717
"""Get all available agent tools"""

src/crewai/tools/agent_tools/ask_question_tool.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Optional
2-
31
from pydantic import BaseModel, Field
42

53
from crewai.tools.agent_tools.base_agent_tools import BaseAgentTool
@@ -21,7 +19,7 @@ def _run(
2119
self,
2220
question: str,
2321
context: str,
24-
coworker: Optional[str] = None,
22+
coworker: str | None = None,
2523
**kwargs,
2624
) -> str:
2725
coworker = self._get_coworker(coworker, **kwargs)

src/crewai/tools/agent_tools/base_agent_tools.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
from typing import Optional
32

43
from pydantic import Field
54

@@ -38,7 +37,7 @@ def sanitize_agent_name(self, name: str) -> str:
3837
# Remove quotes and convert to lowercase
3938
return normalized.replace('"', "").casefold()
4039

41-
def _get_coworker(self, coworker: Optional[str], **kwargs) -> Optional[str]:
40+
def _get_coworker(self, coworker: str | None, **kwargs) -> str | None:
4241
coworker = coworker or kwargs.get("co_worker") or kwargs.get("coworker")
4342
if coworker:
4443
is_list = coworker.startswith("[") and coworker.endswith("]")
@@ -47,10 +46,7 @@ def _get_coworker(self, coworker: Optional[str], **kwargs) -> Optional[str]:
4746
return coworker
4847

4948
def _execute(
50-
self,
51-
agent_name: Optional[str],
52-
task: str,
53-
context: Optional[str] = None
49+
self, agent_name: str | None, task: str, context: str | None = None
5450
) -> str:
5551
"""
5652
Execute delegation to an agent with case-insensitive and whitespace-tolerant matching.
@@ -77,7 +73,9 @@ def _execute(
7773
# when it should look like this:
7874
# {"task": "....", "coworker": "...."}
7975
sanitized_name = self.sanitize_agent_name(agent_name)
80-
logger.debug(f"Sanitized agent name from '{agent_name}' to '{sanitized_name}'")
76+
logger.debug(
77+
f"Sanitized agent name from '{agent_name}' to '{sanitized_name}'"
78+
)
8179

8280
available_agents = [agent.role for agent in self.agents]
8381
logger.debug(f"Available agents: {available_agents}")
@@ -87,38 +85,47 @@ def _execute(
8785
for available_agent in self.agents
8886
if self.sanitize_agent_name(available_agent.role) == sanitized_name
8987
]
90-
logger.debug(f"Found {len(agent)} matching agents for role '{sanitized_name}'")
88+
logger.debug(
89+
f"Found {len(agent)} matching agents for role '{sanitized_name}'"
90+
)
9191
except (AttributeError, ValueError) as e:
9292
# Handle specific exceptions that might occur during role name processing
9393
return self.i18n.errors("agent_tool_unexisting_coworker").format(
9494
coworkers="\n".join(
95-
[f"- {self.sanitize_agent_name(agent.role)}" for agent in self.agents]
95+
[
96+
f"- {self.sanitize_agent_name(agent.role)}"
97+
for agent in self.agents
98+
]
9699
),
97-
error=str(e)
100+
error=str(e),
98101
)
99102

100103
if not agent:
101104
# No matching agent found after sanitization
102105
return self.i18n.errors("agent_tool_unexisting_coworker").format(
103106
coworkers="\n".join(
104-
[f"- {self.sanitize_agent_name(agent.role)}" for agent in self.agents]
107+
[
108+
f"- {self.sanitize_agent_name(agent.role)}"
109+
for agent in self.agents
110+
]
105111
),
106-
error=f"No agent found with role '{sanitized_name}'"
112+
error=f"No agent found with role '{sanitized_name}'",
107113
)
108114

109-
agent = agent[0]
115+
selected_agent = agent[0]
110116
try:
111117
task_with_assigned_agent = Task(
112118
description=task,
113-
agent=agent,
114-
expected_output=agent.i18n.slice("manager_request"),
115-
i18n=agent.i18n,
119+
agent=selected_agent,
120+
expected_output=selected_agent.i18n.slice("manager_request"),
121+
i18n=selected_agent.i18n,
122+
)
123+
logger.debug(
124+
f"Created task for agent '{self.sanitize_agent_name(selected_agent.role)}': {task}"
116125
)
117-
logger.debug(f"Created task for agent '{self.sanitize_agent_name(agent.role)}': {task}")
118-
return agent.execute_task(task_with_assigned_agent, context)
126+
return selected_agent.execute_task(task_with_assigned_agent, context)
119127
except Exception as e:
120128
# Handle task creation or execution errors
121129
return self.i18n.errors("agent_tool_execution_error").format(
122-
agent_role=self.sanitize_agent_name(agent.role),
123-
error=str(e)
130+
agent_role=self.sanitize_agent_name(selected_agent.role), error=str(e)
124131
)

src/crewai/tools/agent_tools/delegate_work_tool.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Optional
2-
31
from pydantic import BaseModel, Field
42

53
from crewai.tools.agent_tools.base_agent_tools import BaseAgentTool
@@ -23,7 +21,7 @@ def _run(
2321
self,
2422
task: str,
2523
context: str,
26-
coworker: Optional[str] = None,
24+
coworker: str | None = None,
2725
**kwargs,
2826
) -> str:
2927
coworker = self._get_coworker(coworker, **kwargs)

src/crewai/tools/base_tool.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import asyncio
22
from abc import ABC, abstractmethod
3+
from collections.abc import Callable
34
from inspect import signature
4-
from typing import Any, Callable, Type, get_args, get_origin, Optional, List
5+
from typing import Any, get_args, get_origin
56

67
from pydantic import (
78
BaseModel,
@@ -19,7 +20,7 @@ class EnvVar(BaseModel):
1920
name: str
2021
description: str
2122
required: bool = True
22-
default: Optional[str] = None
23+
default: str | None = None
2324

2425

2526
class BaseTool(BaseModel, ABC):
@@ -32,10 +33,10 @@ class _ArgsSchemaPlaceholder(PydanticBaseModel):
3233
"""The unique name of the tool that clearly communicates its purpose."""
3334
description: str
3435
"""Used to tell the model how/when/why to use the tool."""
35-
env_vars: List[EnvVar] = []
36+
env_vars: list[EnvVar] = []
3637
"""List of environment variables used by the tool."""
37-
args_schema: Type[PydanticBaseModel] = Field(
38-
default_factory=_ArgsSchemaPlaceholder, validate_default=True
38+
args_schema: type[PydanticBaseModel] = Field(
39+
default=_ArgsSchemaPlaceholder, validate_default=True
3940
)
4041
"""The schema for the arguments that the tool accepts."""
4142
description_updated: bool = False
@@ -52,9 +53,9 @@ class _ArgsSchemaPlaceholder(PydanticBaseModel):
5253
@field_validator("args_schema", mode="before")
5354
@classmethod
5455
def _default_args_schema(
55-
cls, v: Type[PydanticBaseModel]
56-
) -> Type[PydanticBaseModel]:
57-
if not isinstance(v, cls._ArgsSchemaPlaceholder):
56+
cls, v: type[PydanticBaseModel]
57+
) -> type[PydanticBaseModel]:
58+
if v != cls._ArgsSchemaPlaceholder:
5859
return v
5960

6061
return type(
@@ -139,7 +140,7 @@ def from_langchain(cls, tool: Any) -> "BaseTool":
139140
# Infer args_schema from the function signature if not provided
140141
func_signature = signature(tool.func)
141142
annotations = func_signature.parameters
142-
args_fields = {}
143+
args_fields: dict[str, Any] = {}
143144
for name, param in annotations.items():
144145
if name != "self":
145146
param_annotation = (
@@ -247,7 +248,7 @@ def from_langchain(cls, tool: Any) -> "Tool":
247248
# Infer args_schema from the function signature if not provided
248249
func_signature = signature(tool.func)
249250
annotations = func_signature.parameters
250-
args_fields = {}
251+
args_fields: dict[str, Any] = {}
251252
for name, param in annotations.items():
252253
if name != "self":
253254
param_annotation = (

src/crewai/tools/tool_calling.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional
1+
from typing import Any
22

33
from pydantic import BaseModel, Field
44
from pydantic import BaseModel as PydanticBaseModel
@@ -7,7 +7,7 @@
77

88
class ToolCalling(BaseModel):
99
tool_name: str = Field(..., description="The name of the tool to be called.")
10-
arguments: Optional[Dict[str, Any]] = Field(
10+
arguments: dict[str, Any] | None = Field(
1111
..., description="A dictionary of arguments to be passed to the tool."
1212
)
1313

@@ -16,6 +16,6 @@ class InstructorToolCalling(PydanticBaseModel):
1616
tool_name: str = PydanticField(
1717
..., description="The name of the tool to be called."
1818
)
19-
arguments: Optional[Dict[str, Any]] = PydanticField(
19+
arguments: dict[str, Any] | None = PydanticField(
2020
..., description="A dictionary of arguments to be passed to the tool."
2121
)

0 commit comments

Comments
 (0)