1616from pydantic_ai .messages import ToolCallPart
1717from pydantic_ai .models .test import TestModel
1818from pydantic_ai .tools import ToolDefinition
19- from pydantic_ai .toolsets ._dynamic import _DynamicToolset as DynamicToolset
19+ from pydantic_ai .toolsets ._dynamic import _DynamicToolset as DynamicToolset # pyright: ignore[reportPrivateUsage]
2020from pydantic_ai .toolsets .abstract import AbstractToolset , ToolsetTool
2121from pydantic_ai .toolsets .combined import CombinedToolset
2222from pydantic_ai .toolsets .filtered import FilteredToolset
@@ -500,6 +500,25 @@ async def test_context_manager_failed_initialization():
500500 assert server1 .is_running is False
501501
502502
503+
504+ async def test_tool_manager_reuse_self ():
505+ """Test the retry logic with failed_tools and for_run_step method."""
506+
507+ run_context = build_run_context (None , run_step = 1 )
508+
509+ tool_manager = ToolManager [None ](run_context , FunctionToolset [None ](), tools = {})
510+
511+ same_tool_manager = await tool_manager .for_run_step (ctx = run_context )
512+
513+ assert tool_manager is same_tool_manager
514+
515+ step_2_context = build_run_context (None , run_step = 2 )
516+
517+ updated_tool_manager = await tool_manager .for_run_step (ctx = step_2_context )
518+
519+ assert tool_manager != updated_tool_manager
520+
521+
503522async def test_tool_manager_retry_logic ():
504523 """Test the retry logic with failed_tools and for_run_step method."""
505524
@@ -654,7 +673,7 @@ async def get_tools(self, ctx: RunContext[None]) -> dict[str, ToolsetTool[None]]
654673 async def call_tool (
655674 self , name : str , tool_args : dict [str , Any ], ctx : RunContext [None ], tool : ToolsetTool [None ]
656675 ) -> Any :
657- return None
676+ return None # pragma: no cover
658677
659678 def toolset_factory (ctx : RunContext [None ]) -> AbstractToolset [None ]:
660679 return EnterableToolset ()
@@ -686,8 +705,8 @@ def visitor(toolset: AbstractToolset[None]) -> None:
686705
687706 assert tools == {}
688707
689- async def test_dynamic_toolset_empty ():
690708
709+ async def test_dynamic_toolset_empty ():
691710 def no_toolset_func (ctx : RunContext [None ]) -> None :
692711 return None
693712
@@ -697,4 +716,4 @@ def no_toolset_func(ctx: RunContext[None]) -> None:
697716
698717 tools = await toolset .get_tools (run_context )
699718
700- assert tools == {}
719+ assert tools == {}
0 commit comments