Skip to content

chore: Add type checking to test/tools#11284

Merged
sjrl merged 3 commits into
deepset-ai:mainfrom
maxdswain:tools-tests-type-checking
May 13, 2026
Merged

chore: Add type checking to test/tools#11284
sjrl merged 3 commits into
deepset-ai:mainfrom
maxdswain:tools-tests-type-checking

Conversation

@maxdswain
Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

This PR extends type checking to test/tools.

How did you test it?

Re-ran tests - unit, integration and type checkng.

Notes for the reviewer

I didn't want to add WarmupTrackingTool or other classes created just for tests to ToolsType, so I used # type: ignore for these situations.

I modified haystack/tools/__init__.py expanding the ToolsType to include list[ComponentTool] & list[PipelineTool], I think this was the right thing to do but I'm not sure on this.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

@maxdswain maxdswain requested a review from a team as a code owner May 10, 2026 13:53
@maxdswain maxdswain requested review from sjrl and removed request for a team May 10, 2026 13:53
@vercel
Copy link
Copy Markdown

vercel Bot commented May 10, 2026

@maxdswain is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 10, 2026

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/generators/chat
  openai_responses.py 493-494
  haystack/tools
  __init__.py
  from_function.py
Project Total  

This report was generated by python-coverage-comment-action

Comment thread haystack/tools/__init__.py Outdated
# - list[PipelineTool]: List of PipelineTool objects
# - Toolset: Single Toolset (not in a list)
ToolsType = list[Tool] | list[Toolset] | list[Tool | Toolset] | Toolset
ToolsType = list[Tool] | list[Toolset] | list[Tool | Toolset] | list[ComponentTool] | list[PipelineTool] | Toolset
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to explicitly include ComponentTool and PipelineTool since they both inherit from Tool.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get a bunch of type errors in test_component _tool and test_pipeline_tool. This is because lists are invariant meaning that even though they both inherit from Tool, they are not considered subtypes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh okay I see, let me take a deeper look at this

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we try updating this to be ToolsType = Sequence[Tool | Toolset] | Toolset and see if that works? Using Sequence should help us overcome the list invariance issue.

Also @anakin87 I thought we considered using Sequence for this before but can't find the old convo/conclusion on that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That resolves the invariance issues, I've pushed that change in 29e4101 and all your comments should be addressed :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sjrl I tried looking back in the history but my impression is that we never considered Sequence. Happy if it works 🙂

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anakin87 do the changes to this file (addition of the overload) fix the typing issues you were seeing?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes!

from haystack.components.generators.chat.openai import OpenAIChatGenerator
from haystack.tools import tool


@tool
def my_tool(number: int) -> int:
    return number * 2


generator = OpenAIChatGenerator(tools=[my_tool])

running mypy on this example no longer gives errors

Comment thread test/tools/test_searchable_toolset.py Outdated
Comment thread test/tools/test_tools_utils.py Outdated
Comment thread test/tools/test_tools_utils.py Outdated
@sjrl sjrl self-assigned this May 11, 2026
@maxdswain maxdswain force-pushed the tools-tests-type-checking branch from d61e022 to a0cd909 Compare May 12, 2026 16:05
@sjrl
Copy link
Copy Markdown
Contributor

sjrl commented May 13, 2026

@maxdswain thanks! One last comment is could you add a brief release note explaining the update to the ToolsType to mention the changes that were made to it and how that improves type checking?

@maxdswain
Copy link
Copy Markdown
Contributor Author

@maxdswain thanks! One last comment is could you add a brief release note explaining the update to the ToolsType to mention the changes that were made to it and how that improves type checking?

I've now added a release note.

Copy link
Copy Markdown
Contributor

@sjrl sjrl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sjrl sjrl merged commit 972b17a into deepset-ai:main May 13, 2026
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants