Skip to content

Commit a790fd9

Browse files
committed
fix: linting
1 parent 81effe7 commit a790fd9

File tree

4 files changed

+43
-47
lines changed

4 files changed

+43
-47
lines changed

src/strands/tools/decorator.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def my_tool(param1: str, param2: int = 42) -> dict:
6161
from pydantic import BaseModel, Field, create_model
6262
from typing_extensions import override
6363

64-
from ..types.tools import AgentTool, JSONSchema, ToolGenerator, ToolSpec, ToolUse, StrandsContext
64+
from ..types.tools import AgentTool, JSONSchema, StrandsContext, ToolGenerator, ToolSpec, ToolUse
6565

6666
logger = logging.getLogger(__name__)
6767

@@ -107,22 +107,6 @@ def __init__(self, func: Callable[..., Any]) -> None:
107107
# Create a Pydantic model for validation
108108
self.input_model = self._create_input_model()
109109

110-
def _is_special_parameter(self, param_name: str) -> bool:
111-
"""Check if a parameter should be automatically injected by the framework.
112-
113-
Special parameters include:
114-
- Standard Python parameters: self, cls
115-
- Framework-provided context parameters: agent, strands_context
116-
117-
Args:
118-
param_name: The name of the parameter to check.
119-
120-
Returns:
121-
True if the parameter should be excluded from input validation and
122-
automatically injected during tool execution.
123-
"""
124-
return param_name in {"self", "cls", "agent", "strands_context"}
125-
126110
def _create_input_model(self) -> Type[BaseModel]:
127111
"""Create a Pydantic model from function signature for input validation.
128112
@@ -159,6 +143,22 @@ def _create_input_model(self) -> Type[BaseModel]:
159143
# Handle case with no parameters
160144
return create_model(model_name)
161145

146+
def _is_special_parameter(self, param_name: str) -> bool:
147+
"""Check if a parameter should be automatically injected by the framework.
148+
149+
Special parameters include:
150+
- Standard Python parameters: self, cls
151+
- Framework-provided context parameters: agent, strands_context
152+
153+
Args:
154+
param_name: The name of the parameter to check.
155+
156+
Returns:
157+
True if the parameter should be excluded from input validation and
158+
automatically injected during tool execution.
159+
"""
160+
return param_name in {"self", "cls", "agent", "strands_context"}
161+
162162
def extract_metadata(self) -> ToolSpec:
163163
"""Extract metadata from the function to create a tool specification.
164164
@@ -272,10 +272,10 @@ def _inject_special_parameters(
272272
self, validated_input: dict[str, Any], tool_use: ToolUse, invocation_state: dict[str, Any]
273273
) -> None:
274274
"""Inject special framework-provided parameters into the validated input.
275-
275+
276276
This method automatically provides framework-level context to tools that request it
277277
through their function signature.
278-
278+
279279
Args:
280280
validated_input: The validated input parameters (modified in place).
281281
tool_use: The tool use request containing tool invocation details.
@@ -288,7 +288,7 @@ def _inject_special_parameters(
288288
"invocation_state": invocation_state,
289289
}
290290
validated_input["strands_context"] = strands_context
291-
291+
292292
# Inject agent if requested (backward compatibility)
293293
if "agent" in self.signature.parameters and "agent" in invocation_state:
294294
validated_input["agent"] = invocation_state["agent"]

src/strands/types/tools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ class ToolChoiceTool(TypedDict):
119119

120120
class StrandsContext(TypedDict, total=False):
121121
"""Context object containing framework-provided data for decorated tools.
122-
122+
123123
This object provides access to framework-level information that may be useful
124124
for tool implementations. All fields are optional to maintain backward compatibility.
125-
125+
126126
Attributes:
127127
tool_use: The complete ToolUse object containing tool invocation details.
128128
invocation_state: Context for the tool invocation, including agent state.
129129
"""
130-
130+
131131
tool_use: ToolUse
132132
invocation_state: dict[str, Any]
133133

tests/strands/tools/test_decorator.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99

1010
import strands
11-
from strands.types.tools import ToolUse, StrandsContext
11+
from strands.types.tools import StrandsContext, ToolUse
1212

1313

1414
@pytest.fixture(scope="module")
@@ -1041,36 +1041,34 @@ def complex_schema_tool(union_param: Union[List[int], Dict[str, Any], str, None]
10411041
@pytest.mark.asyncio
10421042
async def test_strands_context_injection(alist):
10431043
"""Test that StrandsContext is properly injected into tools that request it."""
1044-
1044+
10451045
@strands.tool
10461046
def context_tool(message: str, strands_context: StrandsContext) -> dict:
10471047
"""Tool that uses StrandsContext to access tool_use_id."""
10481048
tool_use_id = strands_context["tool_use"]["toolUseId"]
10491049
tool_name = strands_context["tool_use"]["name"]
10501050
agent_info = strands_context["invocation_state"].get("agent", "no-agent")
1051-
1051+
10521052
return {
10531053
"status": "success",
1054-
"content": [{"text": f"Tool '{tool_name}' (ID: {tool_use_id}) with agent '{agent_info}' processed: {message}"}]
1054+
"content": [
1055+
{"text": f"Tool '{tool_name}' (ID: {tool_use_id}) with agent '{agent_info}' processed: {message}"}
1056+
],
10551057
}
1056-
1058+
10571059
# Test tool use with context injection
1058-
tool_use = {
1059-
"toolUseId": "test-context-123",
1060-
"name": "context_tool",
1061-
"input": {"message": "hello world"}
1062-
}
1060+
tool_use = {"toolUseId": "test-context-123", "name": "context_tool", "input": {"message": "hello world"}}
10631061
invocation_state = {"agent": "test-agent"}
1064-
1062+
10651063
stream = context_tool.stream(tool_use, invocation_state)
10661064
result = (await alist(stream))[-1]
1067-
1065+
10681066
assert result["status"] == "success"
10691067
assert result["toolUseId"] == "test-context-123"
10701068
assert "Tool 'context_tool' (ID: test-context-123)" in result["content"][0]["text"]
10711069
assert "with agent 'test-agent'" in result["content"][0]["text"]
10721070
assert "processed: hello world" in result["content"][0]["text"]
1073-
1071+
10741072
# Verify strands_context is excluded from schema
10751073
tool_spec = context_tool.tool_spec
10761074
schema_properties = tool_spec["inputSchema"]["json"].get("properties", {})

tests_integ/test_strands_context_integration.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"""
55

66
import logging
7-
from strands import Agent, tool, StrandsContext
7+
8+
from strands import Agent, StrandsContext, tool
89

910
logging.getLogger("strands").setLevel(logging.DEBUG)
1011
logging.basicConfig(format="%(levelname)s | %(name)s | %(message)s", handlers=[logging.StreamHandler()])
@@ -14,33 +15,30 @@
1415
def tool_with_context(message: str, strands_context: StrandsContext) -> dict:
1516
"""Tool that uses StrandsContext to access tool_use_id."""
1617
tool_use_id = strands_context["tool_use"]["toolUseId"]
17-
return {
18-
"status": "success",
19-
"content": [{"text": f"Context tool processed '{message}' with ID: {tool_use_id}"}]
20-
}
18+
return {"status": "success", "content": [{"text": f"Context tool processed '{message}' with ID: {tool_use_id}"}]}
2119

2220

2321
@tool
2422
def tool_with_agent_and_context(message: str, agent, strands_context: StrandsContext) -> dict:
2523
"""Tool that uses both agent and StrandsContext."""
2624
tool_use_id = strands_context["tool_use"]["toolUseId"]
27-
agent_name = getattr(agent, 'name', 'unknown-agent')
25+
agent_name = getattr(agent, "name", "unknown-agent")
2826
return {
29-
"status": "success",
30-
"content": [{"text": f"Agent '{agent_name}' processed '{message}' with ID: {tool_use_id}"}]
27+
"status": "success",
28+
"content": [{"text": f"Agent '{agent_name}' processed '{message}' with ID: {tool_use_id}"}],
3129
}
3230

3331

3432
def test_strands_context_integration():
3533
"""Test StrandsContext functionality with real agent interactions."""
36-
34+
3735
# Initialize agent with tools
3836
agent = Agent(tools=[tool_with_context, tool_with_agent_and_context])
39-
37+
4038
# Test tool with StrandsContext
4139
result1 = agent.tool.tool_with_context(message="hello world")
4240
assert result1.get("status") == "success"
43-
41+
4442
# Test tool with both agent and StrandsContext
4543
result = agent.tool.tool_with_agent_and_context(message="hello agent")
4644
assert result.get("status") == "success"

0 commit comments

Comments
 (0)