Skip to content

Commit a3765f0

Browse files
committed
mypy
1 parent bed8f3f commit a3765f0

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

aws_lambda_powertools/event_handler/bedrock_agent_function.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ def lambda_handler(event, context):
2929
return app.resolve(event, context)
3030
```
3131
"""
32+
3233
def __init__(self) -> None:
3334
self._tools: dict[str, dict[str, Any]] = {}
3435
self.current_event: BedrockAgentFunctionEvent | None = None
3536

3637
def tool(self, description: str | None = None) -> Callable:
3738
"""Decorator to register a tool function"""
39+
3840
def decorator(func: Callable) -> Callable:
3941
if not description:
4042
raise ValueError("Tool description is required")
@@ -48,6 +50,7 @@ def decorator(func: Callable) -> Callable:
4850
"description": description,
4951
}
5052
return func
53+
5154
return decorator
5255

5356
def resolve(self, event: dict[str, Any], context: Any) -> dict[str, Any]:
@@ -60,28 +63,27 @@ def resolve(self, event: dict[str, Any], context: Any) -> dict[str, Any]:
6063

6164
def _resolve(self) -> dict[str, Any]:
6265
"""Internal resolution logic"""
66+
if self.current_event is None:
67+
raise ValueError("No event to process")
68+
6369
function_name = self.current_event.function
6470
action_group = self.current_event.action_group
6571

6672
if function_name not in self._tools:
6773
return self._create_response(
6874
action_group=action_group,
6975
function_name=function_name,
70-
result=f"Function not found: {function_name}"
76+
result=f"Function not found: {function_name}",
7177
)
7278

7379
try:
7480
result = self._tools[function_name]["function"]()
75-
return self._create_response(
76-
action_group=action_group,
77-
function_name=function_name,
78-
result=result
79-
)
81+
return self._create_response(action_group=action_group, function_name=function_name, result=result)
8082
except Exception as e:
8183
return self._create_response(
8284
action_group=action_group,
8385
function_name=function_name,
84-
result=f"Error: {str(e)}"
86+
result=f"Error: {str(e)}",
8587
)
8688

8789
def _create_response(self, action_group: str, function_name: str, result: Any) -> dict[str, Any]:
@@ -91,12 +93,6 @@ def _create_response(self, action_group: str, function_name: str, result: Any) -
9193
"response": {
9294
"actionGroup": action_group,
9395
"function": function_name,
94-
"functionResponse": {
95-
"responseBody": {
96-
"TEXT": {
97-
"body": str(result)
98-
}
99-
}
100-
}
101-
}
102-
}
96+
"functionResponse": {"responseBody": {"TEXT": {"body": str(result)}}},
97+
},
98+
}

tests/functional/event_handler/required_dependencies/test_bedrock_agent_functions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import pytest
4+
45
from aws_lambda_powertools.event_handler import BedrockAgentFunctionResolver
56
from aws_lambda_powertools.utilities.data_classes import BedrockAgentFunctionEvent
67
from tests.functional.utils import load_event
@@ -74,6 +75,7 @@ def test_bedrock_agent_function_missing_description():
7475
# WHEN registering a tool without description
7576
# THEN raise ValueError
7677
with pytest.raises(ValueError, match="Tool description is required"):
78+
7779
@app.tool()
7880
def test_function():
7981
return "test"
@@ -90,6 +92,7 @@ def test_function():
9092

9193
# THEN raise ValueError on second registration
9294
with pytest.raises(ValueError, match="Tool 'test_function' already registered"):
95+
9396
@app.tool(description="Second registration")
9497
def test_function(): # noqa: F811
9598
return "test"
@@ -106,4 +109,4 @@ def test_function():
106109
# WHEN calling with invalid event
107110
# THEN raise ValueError
108111
with pytest.raises(ValueError, match="Missing required field"):
109-
app.resolve({}, {})
112+
app.resolve({}, {})

0 commit comments

Comments
 (0)