Skip to content

Commit 29e4101

Browse files
committed
chore: Update ToolsType to use Sequence
1 parent a0cd909 commit 29e4101

4 files changed

Lines changed: 16 additions & 19 deletions

File tree

haystack/components/generators/chat/openai_responses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def _prepare_api_call( # noqa: PLR0913
488488
# Convert all tool objects to the correct OpenAI-compatible structure
489489
else:
490490
# mypy can't infer that tools is ToolsType here
491-
flattened_tools = flatten_tools_or_toolsets(tools)
491+
flattened_tools = flatten_tools_or_toolsets(tools) # type: ignore[arg-type]
492492
_check_duplicate_tool_names(flattened_tools)
493493
for t in flattened_tools:
494494
function_spec = {**t.tool_spec}

haystack/tools/__init__.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
# ruff: noqa: I001 (ignore import order as we need to import Tool before ComponentTool and PipelineTool)
88

9+
from collections.abc import Sequence
910
from haystack.tools.from_function import create_tool_from_function, tool
1011
from haystack.tools.tool import Tool, _check_duplicate_tool_names
1112
from haystack.tools.toolset import Toolset
@@ -15,15 +16,11 @@
1516
from haystack.tools.serde_utils import deserialize_tools_or_toolset_inplace, serialize_tools_or_toolset
1617
from haystack.tools.utils import flatten_tools_or_toolsets, warm_up_tools
1718

18-
# Type alias for tools parameter - allows mixing Tools and Toolsets in a list
19-
# Explicitly list all valid combinations due to list invariance:
20-
# - list[Tool]: Most common pattern - list of Tool objects
21-
# - list[Toolset]: Less common pattern - list of Toolset objects
22-
# - list[Union[Tool, Toolset]]: Mixing Tools and Toolsets in one list
23-
# - list[ComponentTool]: List of ComponentTool objects
24-
# - list[PipelineTool]: List of PipelineTool objects
25-
# - Toolset: Single Toolset (not in a list)
26-
ToolsType = list[Tool] | list[Toolset] | list[Tool | Toolset] | list[ComponentTool] | list[PipelineTool] | Toolset
19+
# Type alias for tools parameter - allows mixing Tools and Toolsets in a sequence
20+
# Accepts either:
21+
# - Sequence[Tool | Toolset]: Any sequence (list, tuple, etc.) containing Tools, Toolsets, or a mix of both
22+
# - Toolset: A single Toolset (not in a sequence)
23+
ToolsType = Sequence[Tool | Toolset] | Toolset
2724

2825
__all__ = [
2926
"_check_duplicate_tool_names",

test/tools/test_searchable_toolset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def test_init_with_invalid_catalog(self):
102102
with pytest.raises(TypeError):
103103
SearchableToolset(catalog=123) # type: ignore[arg-type]
104104
with pytest.raises(TypeError):
105-
SearchableToolset(catalog=[123]) # type: ignore[arg-type]
105+
SearchableToolset(catalog=[123]) # type: ignore[list-item]
106106
with pytest.raises(TypeError):
107107
SearchableToolset(
108108
catalog=Tool( # type: ignore[arg-type]
@@ -682,7 +682,7 @@ def warm_up(self) -> None:
682682
for i in range(5)
683683
]
684684

685-
toolset = SearchableToolset(catalog=[LazyToolset()] + eager_tools) # type: ignore[arg-type]
685+
toolset = SearchableToolset(catalog=[LazyToolset(), *eager_tools])
686686
toolset.warm_up()
687687

688688
# Should have 5 lazy + 5 eager = 10 tools

test/tools/test_tools_utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ def test_flatten_list_with_empty_toolsets(self, add_tool):
140140
def test_flatten_invalid_type_in_list(self):
141141
"""Test that invalid types in the list raise TypeError."""
142142
with pytest.raises(TypeError, match="Items in the tools list must be Tool or Toolset instances"):
143-
flatten_tools_or_toolsets(["not_a_tool"]) # type: ignore[arg-type]
143+
flatten_tools_or_toolsets(["not_a_tool"]) # type: ignore[list-item]
144144

145145
with pytest.raises(TypeError, match="Items in the tools list must be Tool or Toolset instances"):
146-
flatten_tools_or_toolsets([123]) # type: ignore[arg-type]
146+
flatten_tools_or_toolsets([123]) # type: ignore[list-item]
147147

148148
with pytest.raises(TypeError, match="Items in the tools list must be Tool or Toolset instances"):
149-
flatten_tools_or_toolsets([{"key": "value"}]) # type: ignore[arg-type]
149+
flatten_tools_or_toolsets([{"key": "value"}]) # type: ignore[list-item]
150150

151151
def test_flatten_invalid_type(self):
152152
"""Test that invalid root types raise TypeError."""
@@ -215,7 +215,7 @@ def test_warm_up_tools_with_single_tool(self):
215215
)
216216

217217
assert not tool.was_warmed_up
218-
warm_up_tools([tool]) # type: ignore[arg-type]
218+
warm_up_tools([tool])
219219
assert tool.was_warmed_up
220220

221221
def test_warm_up_tools_with_single_toolset(self):
@@ -270,7 +270,7 @@ def test_warm_up_tools_with_list_containing_toolset(self):
270270
assert not tool1.was_warmed_up
271271
assert not tool2.was_warmed_up
272272

273-
warm_up_tools([toolset]) # type: ignore[arg-type]
273+
warm_up_tools([toolset])
274274

275275
# Both the toolset itself and individual tools should be warmed up
276276
assert toolset.was_warmed_up
@@ -307,7 +307,7 @@ def test_warm_up_tools_with_multiple_toolsets(self):
307307
assert not tool2.was_warmed_up
308308
assert not tool3.was_warmed_up
309309

310-
warm_up_tools([toolset1, toolset2]) # type: ignore[arg-type]
310+
warm_up_tools([toolset1, toolset2])
311311

312312
# Both toolsets and all individual tools should be warmed up
313313
assert toolset1.was_warmed_up
@@ -344,7 +344,7 @@ def test_warm_up_tools_with_mixed_tools_and_toolsets(self):
344344
assert not toolset_tool1.was_warmed_up
345345
assert not toolset_tool2.was_warmed_up
346346

347-
warm_up_tools([standalone_tool, toolset]) # type: ignore[arg-type]
347+
warm_up_tools([standalone_tool, toolset])
348348

349349
# All tools and the toolset should be warmed up
350350
assert standalone_tool.was_warmed_up

0 commit comments

Comments
 (0)