Skip to content

fix: support negative indices in _ToolsetWrapper.__getitem__#11760

Open
immuhammadfurqan wants to merge 2 commits into
deepset-ai:mainfrom
immuhammadfurqan:fix/toolset-wrapper-negative-index
Open

fix: support negative indices in _ToolsetWrapper.__getitem__#11760
immuhammadfurqan wants to merge 2 commits into
deepset-ai:mainfrom
immuhammadfurqan:fix/toolset-wrapper-negative-index

Conversation

@immuhammadfurqan

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

Toolset is documented as implementing the collection interface (__iter__, __contains__, __len__, __getitem__) so it "behaves like a list of Tools." Toolset.__getitem__ delegates to a real list, so negative indices work. But _ToolsetWrapper (returned when combining Toolsets with +) only did a forward enumerate scan in __getitem__, so combined[-1] always raised IndexError, breaking that documented contract as soon as two toolsets are combined.

This PR changes _ToolsetWrapper.__getitem__ to materialize list(self) (reusing the existing __iter__, which already flattens all wrapped toolsets) and index into that, so it behaves exactly like a list, including negative indices and the standard out-of-range IndexError.

How did you test it?

Added a regression test covering positive indices, negative indices, and the out-of-range case on a wrapper created by combining two Toolsets. Ran the full test/tools/test_toolset_wrapper.py and test/tools/test_toolset.py suites (23 passed), ruff check/ruff format, and mypy — all clean.

Notes for the reviewer

None.

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.

_ToolsetWrapper (returned when combining Toolsets with +) only did a
forward enumerate scan in __getitem__, so negative indices always
raised IndexError, unlike a plain Toolset which delegates to a real
list. Reuse __iter__ via list(self)[index] so indexing behaves
consistently with a list of Tools, as documented.

Fixes deepset-ai#11759
@immuhammadfurqan immuhammadfurqan requested a review from a team as a code owner June 24, 2026 20:06
@immuhammadfurqan immuhammadfurqan requested review from julian-risch and removed request for a team June 24, 2026 20:06
@vercel

vercel Bot commented Jun 24, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Jun 24, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added the type:documentation Improvements on the docs label Jun 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/tools
  toolset.py
Project Total  

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_ToolsetWrapper.__getitem__ doesn't support negative indices, unlike Toolset

2 participants