Files
Stefano Fiorucci 7c6323b960 refactor: support PEP604 typing syntax (X|Y and X|None) (#10255)
* adopt X|Y syntax: draft

* cast Union

* fix pylint + state testing

* use X|Y

* rm unused imports

* trigger e2e tests

* fix + simplification

* add compatibility tests

* rm e2e tests trigger

* fix

* add relnote

* simplify/fix pep604 union parsing

* fix comments

* test _is_optional_type

* introduce _build_pep604_union_type; make _is_union_type private

* try removing problematic test
2025-12-19 15:47:51 +01:00

65 lines
2.0 KiB
Python

# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
from typing import TYPE_CHECKING
from haystack.tools.tool import Tool
from haystack.tools.toolset import Toolset
if TYPE_CHECKING:
from haystack.tools import ToolsType
def warm_up_tools(tools: "ToolsType | None" = None) -> None:
"""
Warm up tools from various formats (Tools, Toolsets, or mixed lists).
For Toolset objects, this delegates to Toolset.warm_up(), which by default
warms up all tools in the Toolset. Toolset subclasses can override warm_up()
to customize initialization behavior (e.g., setting up shared resources).
:param tools: A list of Tool and/or Toolset objects, a single Toolset, or None.
"""
if tools is None:
return
# If tools is a single Toolset or Tool, warm it up
if isinstance(tools, (Toolset, Tool)):
if hasattr(tools, "warm_up"):
tools.warm_up()
return
# If tools is a list, warm up each item (Tool or Toolset)
if isinstance(tools, list):
for item in tools:
if hasattr(item, "warm_up"):
item.warm_up()
def flatten_tools_or_toolsets(tools: "ToolsType | None") -> list[Tool]:
"""
Flatten tools from various formats into a list of Tool instances.
:param tools: Tools in list[Union[Tool, Toolset]], Toolset, or None format.
:returns: A flat list of Tool instances.
"""
if tools is None:
return []
if isinstance(tools, Toolset):
return list(tools)
if isinstance(tools, list):
flattened: list[Tool] = []
for item in tools:
if isinstance(item, Toolset):
flattened.extend(list(item))
elif isinstance(item, Tool):
flattened.append(item)
else:
raise TypeError("Items in the tools list must be Tool or Toolset instances.")
return flattened
raise TypeError("tools must be list[Union[Tool, Toolset]], Toolset, or None")