Skip to content

Type errors due tolist[Tool] -> ToolsType change #9949

@denisw

Description

@denisw

Describe the bug

In v2.19, Haystack changed the type of the tools parameter from list[Tool] to list[Tool | Toolset] in multiple places, including the Agent and ToolsInvoker constructor. Unfortunately, this is a breaking change to the typings because List is invariant. This means that for example, the following code does not type-check because even though Tool is a subtype of Tool | Toolset, list[Tool] is not a subtype of list[Tool | Toolset]:

tool1: Tool = ...
tool2: Tool = ...
tools = [tool1, tool2]
invoker = ToolInvoker(tools=tools)

Error message

From Pyright:

error: Argument of type "list[Tool]" cannot be assigned to parameter "tools" of type "ToolsType" in function "__init__"
    Type "list[Tool]" is not assignable to type "ToolsType"
      "list[Tool]" is not assignable to "list[Tool | Toolset]"
        Type parameter "_T@list" is invariant, but "Tool" is not the same as "Tool | Toolset"
        Consider switching from "list" to "Sequence" which is covariant
      "list[Tool]" is not assignable to "Toolset" (reportArgumentType)

Expected behavior

No type error, as before.

Additional context

A solution could be to accept a Sequence[Tool | Toolset] instead, which would work because Sequence is covariant. However, this means that the implementation either has to work with tuples and other types of sequences or manually converts the parameter to a list using list(tools).

To Reproduce

See the code snippet above. I used Pyright v1.1.407.

FAQ Check

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions