Skip to content

Run typing checks on our test files #10396

Description

@sjrl

While working on changes to haystack-experimental (deepset-ai/haystack-experimental#430) I wanted to ensure that the protocol I was developing was working as expected with the respective implementations.

I realized though that we never actually test whether our implementations satisfy our protocols since we don't do any type checks on actual test-like code.

Therefore, I think it would be a good idea if we add type checking to our test code in our repos so we can better catch these issues. This will be quite a bit work to get our tests fully compliant but perhaps we could do it in a piece-meal approach where we only turn on type checking in a small part of tests and grow from there.

Components

  • test/components/audio/
  • test/components/builders/
  • test/components/caching/
  • test/components/classifiers/
  • test/components/connectors/
  • test/components/converters/
  • test/components/embedders/
  • test/components/evaluators/
  • test/components/extractors/
  • test/components/fetchers/
  • test/components/generators/
  • test/components/joiners/
  • test/components/preprocessors/
  • test/components/query/
  • test/components/rankers/
  • test/components/readers/
  • test/components/retrievers/
  • test/components/routers/
  • test/components/samplers/
  • test/components/tools/
  • test/components/validators/
  • test/components/websearch/
  • test/components/writers/

Document Stores

  • test/document_stores/

Agents

  • test/components/agents/

Other

  • test/core/
  • test/dataclasses/
  • test/evaluation/
  • test/fuzz/
  • test/hooks/
  • test/human_in_the_loop/
  • test/marshal/
  • test/skill_stores/
  • test/testing/
  • test/token_counters/
  • test/tools/ (top-level, distinct from test/components/tools/)
  • test/tracing/
  • test/utils/

Activity

  1. added
    P2
    on Jan 16, 2026
  2. removed their assignment
    on Jan 29, 2026
  3. Jventajas commented on Jul 1, 2026

    @Jventajas

    Hi, is this still relevant? Should I take it?

  4. omborda2002 commented on Jul 3, 2026

    @omborda2002
    Contributor

    I took the next piece of this, test/components/embedders/. Most of the errors there were tests passing a wrong type on purpose to check the raised TypeError, plus a few missing annotations. Opening a PR now.

  5. yaodong-shen commented on Aug 2, 2026

    @yaodong-shen
    Contributor

    I can take the next small increment for this issue: add test/components/caching/ to the existing mypy target, fix only the type errors needed in its two CacheChecker test modules, and run the focused mypy/pytest checks before opening a PR. I checked that the existing recent contribution covered test/components/embedders/; I will keep this scope disjoint.

  6. removed their assignment
    on Aug 5, 2026
  7. Aryan-Pardeshi commented on Aug 7, 2026

    @Aryan-Pardeshi
    Contributor

    Working on adding typing checks for \ est/components/writers/!

  8. 14 remaining items

  9. ShousenZHANG commented on Aug 25, 2026

    @ShousenZHANG
    Contributor

    I can take the next disjoint increment: test/components/builders/.

    It is not in the current types target on main, and no open or closed PR or comment covers it. The increments in flight are extractors/ (#12343), samplers/ (#12433) and fetchers/ (#12435), so this doesn't overlap any of them.

    Baseline is 25 errors across 2 of the 3 test modules:

    • 12 attr-defined on __haystack_input__ / __haystack_output__, which the @component decorator attaches at runtime.
    • 8 index / union-attr from reading builder.template[0].text, where template is typed list[ChatMessage] | str | None. Those get narrowed with an assert rather than silenced.
    • 1 list-item on a deliberate mixed-type template inside a validation test, so that one gets a narrow # type: ignore[list-item].
    • 2 no-untyped-def for missing return annotations.

    Scoped to the minimal typing fixes plus the types target entry, with no change to what any test asserts.

    Note for whoever picks the next one: every increment edits the same single types = "..." line in pyproject.toml, so whichever lands first leaves the others conflicting. #12433 and #12435 are both showing conflicts right now and just need a rebase of that one line.

  10. ShousenZHANG commented on Aug 27, 2026

    @ShousenZHANG
    Contributor

    I can take the next disjoint increment: test/components/retrievers/.

    It is not in the current types target on main, and no open or closed PR or comment covers it. It also doesn't overlap fetchers/ (#12435), the only increment still in flight.

    Baseline is 78 errors across 11 of the 16 test modules, concentrated in three files:

    • test_auto_merging_retriever.py / _async.py (20 each) — mostly call-overload from a docs variable that is first the dict returned by HierarchicalDocumentSplitter.run and then reassigned to a retriever result, so mypy can't keep one type for it. Fixed by giving the two values separate names.
    • test_multi_retriever.py (18) — no-untyped-def and assignment.
    • The remaining 8 files have 1-7 errors each: type-var on sorted over float | None, method-assign on monkeypatched to_dict, and a no-redef on a re-annotated local.

    Scoped to the minimal typing fixes plus the types target entry, with no change to what any test asserts.

  11. ShousenZHANG commented on Aug 27, 2026

    @ShousenZHANG
    Contributor

    I can take test/components/extractors/ next. It doesn't overlap fetchers/ (#12435), the only other increment in flight.

    One thing worth flagging: this directory is already clean. mypy test/components/extractors/ reports Success: no issues found in 5 source files against current main, but the path was never added to the types target, so CI isn't actually checking it — any typing regression introduced there today would go unnoticed.

    So this increment is a one-line change to pyproject.toml, with no source edits needed. Happy to fold it into someone else's PR instead if that's tidier; it seemed worth closing the gap either way.

  12. ShousenZHANG commented on Aug 29, 2026

    @ShousenZHANG
    Contributor

    I can take the next disjoint increment: test/components/preprocessors/.

    It is not in the current types target on main, and no open or closed PR or comment covers it — fetchers/ (#12435) and retrievers/ (#12481) are the only increments in flight, and neither overlaps.

    Baseline is 265 errors across 9 of the 13 files, but that number is misleading. 206 of them are in test_recursive_splitter.py alone, and they all cascade from one pattern repeated seven times, where the dict returned by run() is rebound to the list it holds:

    doc_chunks = splitter.run([doc])
    doc_chunks = doc_chunks["documents"]

    Giving the two values separate names drops that file to 26 and the directory to 85 — in line with the recent increments.

    Nine open PRs add tests to these files, but all of them are pure additions; a three-way merge against each produces zero conflicts, so this shouldn't step on any of them.

    Opened #12504.

  13. ShousenZHANG commented on Sep 11, 2026

    @ShousenZHANG
    Contributor

    Taking test/hooks/budget/ — the smallest of the test/hooks/ gaps. The types target currently covers only test/hooks/human_in_the_loop, so budget/, compaction/, tool_result_offloading/ and the three test/hooks/*.py root files are all still unchecked.

    Baseline is 3 errors in test_hooks.py, and neither group is a blanket-annotation case:

    • KeepIterating, the inline stub passed as an on_exit hook, is missing the to_dict / from_dict members the Hook protocol requires (list-item at L106). Implementing them via default_to_dict / default_from_dict matches the LifecycleHook stubs test: type-check agent hook and confirmation tests #12681 just added in test/components/agents/test_agent_hooks.py.
    • the two agent.chat_generator.run = MagicMock(...) lines are method-assign. Replacing a bound method on a mock generator is the intended pattern here, so those get a narrow # type: ignore[method-assign] rather than a restructure.

    No overlap with compaction/, tool_result_offloading/ or the agents/ / converters/ remainders — happy to take another of those next if nobody else has.

  14. ShousenZHANG commented on Sep 11, 2026

    @ShousenZHANG
    Contributor

    Taking the three test/hooks/ root modules next — test_from_function.py, test_invocation.py, test_utils.py — as a follow-up to #12710 (test/hooks/budget/).

    Baseline is 56 errors, but 44 of them come from a single cause: six inline stub hooks (RecordingHook, ThreadRecordingHook, AsyncRecordingHook, LifecycleSpy, WarmOnlyHook, PlainHook) are missing the to_dict / from_dict members the Hook protocol requires, so every list they appear in fails list-item. Implementing the pair via default_to_dict / default_from_dict — the shape #12681 used for LifecycleHook — clears all 44.

    The rest: a handful of hook dictionaries inferred as dict[str, ...] where the callee wants HookPoint keys, fixed with an explicit annotation; and one .function read off a Hook, narrowed with isinstance rather than suppressed.

    Five suppressions remain, all in TestFunctionHookConstruction, where the test deliberately passes a wrong callable to assert FunctionHook rejects it — there the type error is the thing under test.

    Still not touching compaction/ or tool_result_offloading/; those are the larger remaining test/hooks/ gaps and I'd rather land these two first.

  15. Anurag-M1 commented on Sep 12, 2026

    @Anurag-M1
    Contributor

    Taking test/hooks/tool_result_offloading/.

    Opened #12720 with the minimal typing fixes plus the types target entry in pyproject.toml:

    • Added test/hooks/tool_result_offloading/ to the types script line in pyproject.toml.
    • In test/hooks/tool_result_offloading/test_policies.py: added isinstance(restored, OffloadOverChars) assertion to narrow the restored policy before accessing .threshold.
    • In test/hooks/tool_result_offloading/test_hooks.py:
      • Updated TextOnlyToolResultStore.write parameter content to str | bytes to satisfy ToolResultStore protocol under LSP.
      • Added explicit type annotation for content: list[TextContent | ImageContent | FileContent] in test cases.
      • Added narrow # type: ignore[method-assign] and # type: ignore[attr-defined] for mock generator setup.

    Passed all 40 unit tests, hatch run test:types (508 source files), and hatch run fmt-check locally.

  16. removed their assignment
    on Sep 24, 2026
  17. ShousenZHANG commented on Sep 24, 2026

    @ShousenZHANG
    Contributor

    Taking the last piece. After #12758 and #12761, the only tracked .py files under test/ that the types target still skips are test/conftest.py and three __init__.py files holding nothing but the license header. The unchecked directories in the list above (classifiers/, connectors/, readers/, tools/, websearch/) no longer exist on main.

    So this switches the target to {args:haystack test} and drops the # TODO We want to eventually type the whole test folder. Baseline is 3 errors, all in conftest.py:

    • request_blocker is missing its monkeypatch: pytest.MonkeyPatch and -> None annotations.
    • spying_tracer sets tracer.is_content_tracing_enabled = True on a SpyingTracer, which has no such attribute. The flag lives on the global ProxyTracer (haystack/tracing/tracer.py:118, read at :65), and SpyingSpan.set_content_tag records unconditionally, so the line has no effect and goes.

    Opening the PR shortly.

  18. bogdankostic commented on Sep 25, 2026

    @bogdankostic
    Contributor

    #12930 replaced the hand-maintained path list with {args:haystack test}, so the whole test/ folder is now type-checked.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions