Apply automatic linting fixes to tests directory

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-05-12 13:31:07 +00:00
parent ad1ea46bbb
commit 46621113af
62 changed files with 1738 additions and 1821 deletions

View File

@@ -1,13 +1,11 @@
import asyncio
import inspect
import unittest
from typing import Any, Callable, Dict, List
from collections.abc import Callable
from unittest.mock import patch
from crewai.tools import BaseTool, tool
def test_creating_a_tool_using_annotation():
def test_creating_a_tool_using_annotation() -> None:
@tool("Name of my tool")
def my_tool(question: str) -> str:
"""Clear description for what this tool is useful for, your agent will need this information to use it."""
@@ -20,7 +18,7 @@ def test_creating_a_tool_using_annotation():
== "Tool Name: Name of my tool\nTool Arguments: {'question': {'description': None, 'type': 'str'}}\nTool Description: Clear description for what this tool is useful for, your agent will need this information to use it."
)
assert my_tool.args_schema.model_json_schema()["properties"] == {
"question": {"title": "Question", "type": "string"}
"question": {"title": "Question", "type": "string"},
}
assert (
my_tool.func("What is the meaning of life?") == "What is the meaning of life?"
@@ -34,7 +32,7 @@ def test_creating_a_tool_using_annotation():
== "Tool Name: Name of my tool\nTool Arguments: {'question': {'description': None, 'type': 'str'}}\nTool Description: Clear description for what this tool is useful for, your agent will need this information to use it."
)
assert converted_tool.args_schema.model_json_schema()["properties"] == {
"question": {"title": "Question", "type": "string"}
"question": {"title": "Question", "type": "string"},
}
assert (
converted_tool.func("What is the meaning of life?")
@@ -42,7 +40,7 @@ def test_creating_a_tool_using_annotation():
)
def test_creating_a_tool_using_baseclass():
def test_creating_a_tool_using_baseclass() -> None:
class MyCustomTool(BaseTool):
name: str = "Name of my tool"
description: str = "Clear description for what this tool is useful for, your agent will need this information to use it."
@@ -59,7 +57,7 @@ def test_creating_a_tool_using_baseclass():
== "Tool Name: Name of my tool\nTool Arguments: {'question': {'description': None, 'type': 'str'}}\nTool Description: Clear description for what this tool is useful for, your agent will need this information to use it."
)
assert my_tool.args_schema.model_json_schema()["properties"] == {
"question": {"title": "Question", "type": "string"}
"question": {"title": "Question", "type": "string"},
}
assert my_tool.run("What is the meaning of life?") == "What is the meaning of life?"
@@ -71,7 +69,7 @@ def test_creating_a_tool_using_baseclass():
== "Tool Name: Name of my tool\nTool Arguments: {'question': {'description': None, 'type': 'str'}}\nTool Description: Clear description for what this tool is useful for, your agent will need this information to use it."
)
assert converted_tool.args_schema.model_json_schema()["properties"] == {
"question": {"title": "Question", "type": "string"}
"question": {"title": "Question", "type": "string"},
}
assert (
converted_tool._run("What is the meaning of life?")
@@ -79,7 +77,7 @@ def test_creating_a_tool_using_baseclass():
)
def test_setting_cache_function():
def test_setting_cache_function() -> None:
class MyCustomTool(BaseTool):
name: str = "Name of my tool"
description: str = "Clear description for what this tool is useful for, your agent will need this information to use it."
@@ -93,7 +91,7 @@ def test_setting_cache_function():
assert not my_tool.cache_function()
def test_default_cache_function_is_true():
def test_default_cache_function_is_true() -> None:
class MyCustomTool(BaseTool):
name: str = "Name of my tool"
description: str = "Clear description for what this tool is useful for, your agent will need this information to use it."
@@ -106,30 +104,31 @@ def test_default_cache_function_is_true():
assert my_tool.cache_function()
def test_result_as_answer_in_tool_decorator():
def test_result_as_answer_in_tool_decorator() -> None:
@tool("Tool with result as answer", result_as_answer=True)
def my_tool_with_result_as_answer(question: str) -> str:
"""This tool will return its result as the final answer."""
return question
assert my_tool_with_result_as_answer.result_as_answer is True
converted_tool = my_tool_with_result_as_answer.to_structured_tool()
assert converted_tool.result_as_answer is True
@tool("Tool with default result_as_answer")
def my_tool_with_default(question: str) -> str:
"""This tool uses the default result_as_answer value."""
return question
assert my_tool_with_default.result_as_answer is False
converted_tool = my_tool_with_default.to_structured_tool()
assert converted_tool.result_as_answer is False
class SyncTool(BaseTool):
"""Test implementation with a synchronous _run method"""
"""Test implementation with a synchronous _run method."""
name: str = "sync_tool"
description: str = "A synchronous tool for testing"
@@ -139,7 +138,8 @@ class SyncTool(BaseTool):
class AsyncTool(BaseTool):
"""Test implementation with an asynchronous _run method"""
"""Test implementation with an asynchronous _run method."""
name: str = "async_tool"
description: str = "An asynchronous tool for testing"
@@ -149,7 +149,7 @@ class AsyncTool(BaseTool):
return f"Processed {input_text} asynchronously"
def test_sync_run_returns_direct_result():
def test_sync_run_returns_direct_result() -> None:
"""Test that _run in a synchronous tool returns a direct result, not a coroutine."""
tool = SyncTool()
result = tool._run(input_text="hello")
@@ -161,7 +161,7 @@ def test_sync_run_returns_direct_result():
assert run_result == "Processed hello synchronously"
def test_async_run_returns_coroutine():
def test_async_run_returns_coroutine() -> None:
"""Test that _run in an asynchronous tool returns a coroutine object."""
tool = AsyncTool()
result = tool._run(input_text="hello")
@@ -170,11 +170,11 @@ def test_async_run_returns_coroutine():
result.close() # Clean up the coroutine
def test_run_calls_asyncio_run_for_async_tools():
def test_run_calls_asyncio_run_for_async_tools() -> None:
"""Test that asyncio.run is called when using async tools."""
async_tool = AsyncTool()
with patch('asyncio.run') as mock_run:
with patch("asyncio.run") as mock_run:
mock_run.return_value = "Processed test asynchronously"
async_result = async_tool.run(input_text="test")
@@ -182,11 +182,11 @@ def test_run_calls_asyncio_run_for_async_tools():
assert async_result == "Processed test asynchronously"
def test_run_does_not_call_asyncio_run_for_sync_tools():
def test_run_does_not_call_asyncio_run_for_sync_tools() -> None:
"""Test that asyncio.run is NOT called when using sync tools."""
sync_tool = SyncTool()
with patch('asyncio.run') as mock_run:
with patch("asyncio.run") as mock_run:
sync_result = sync_tool.run(input_text="test")
mock_run.assert_not_called()