Skip to content

test: enable mypy typing checks for test/components/routers/ - #12354

Open
atikulmunna wants to merge 1 commit into
deepset-ai:mainfrom
atikulmunna:type/enable-mypy-for-test-components-routers
Open

test: enable mypy typing checks for test/components/routers/#12354
atikulmunna wants to merge 1 commit into
deepset-ai:mainfrom
atikulmunna:type/enable-mypy-for-test-components-routers

Conversation

@atikulmunna

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

Adds test/components/routers/ to the mypy target and fixes the 63 errors it surfaced.

Most of them came from route lists being inferred as list[dict[str, object]] where list[Route] is expected. Annotating those literals clears 26 errors by itself, and because Route is a TypedDict it also means mypy now checks each route against the real schema (condition, output, output_name, output_type) rather than accepting any dict.

The routes that are deliberately malformed keep a narrow type: ignore naming the specific violation, for example typeddict-item for a missing key and typeddict-unknown-key for an extra one. Being wrong is what those tests assert, and the error code documents which kind of wrong.

Three other things:

  • test_metadata_router now asserts the concrete type of routed items before reading .data or .content. That narrows the Document | ByteStream union and also covers something the tests did not previously check.
  • test_file_router called output.get(key) and then len() on the result. Those assertions require the key to be present, so they now index directly. It types cleanly and fails more clearly when a key is missing.
  • __haystack_input__ / __haystack_output__ keep type: ignore[attr-defined], matching how test/core/component/test_component.py and test/components/generators/chat/test_llm.py already handle the attributes the @component decorator adds at runtime.

How did you test it?

All through hatch, on Python 3.10:

  • hatch run test:types gives Success: no issues found in 434 source files
  • hatch run fmt-check gives All checks passed!
  • hatch run test:unit test/components/routers gives 104 passed, 30 skipped
  • pre-commit run over the changed files: all applicable hooks pass

Notes for the reviewer

Annotating the routes surfaced two signatures that look narrower than the code they describe. I have not touched either, since both are public and this is a test-only change, but they may be worth a follow-up:

1. Route.output_type is type | list[type], which does not cover everything ConditionalRouter accepts. conditional_router.py has an explicit branch for "Union types (including Optional and X | Y syntax)", and there are passing tests using Callable[[int, str], bool] and list[str] | dict[str, int] | None as an output_type. Neither is a type, so both need an ignore today.

2. MetadataRouter.run takes list[Document] | list[ByteStream], which excludes a mixed list. test_run_with_mixed_documents_and_byte_streams passes exactly that and it works at runtime, so that call needs an ignore too.

Happy to open a PR for either if you think the annotations should be widened.

Checklist

Part of deepset-ai#10396.

63 errors to zero. The bulk came from route lists being inferred as
list[dict[str, object]] where list[Route] is expected. Annotating those
literals fixes 26 errors on its own, and since Route is a TypedDict it
also makes mypy check every route against the real schema instead of
accepting any dict.

The routes that are deliberately malformed, missing keys, an int where a
template string belongs, an extra key, keep a narrow type: ignore naming
the specific violation, since being wrong is what those tests assert.

Other changes worth noting:

* test_metadata_router now asserts the concrete type of routed items
  before reading .data or .content, which narrows the Document | ByteStream
  union and also checks something the tests did not check before.
* test_file_router used output.get(key) then called len() on the result.
  Those assertions require the key to be present, so they now index
  directly, which types cleanly and fails more clearly if a key is missing.
* __haystack_input__ / __haystack_output__ keep type: ignore[attr-defined],
  matching how test/core/component/test_component.py and
  test/components/generators/chat/test_llm.py already handle them.

Two signatures look narrower than the code they describe. Both are left
alone here and noted in the PR description as possible follow-ups.

Testing:

* hatch run test:types: Success, no issues found in 434 source files
* hatch run fmt-check: All checks passed
* hatch run test:unit test/components/routers: 104 passed, 30 skipped
* pre-commit on the changed files: all applicable hooks pass
@atikulmunna
atikulmunna requested a review from a team as a code owner August 14, 2026 19:04
@atikulmunna
atikulmunna requested review from bogdankostic and removed request for a team August 14, 2026 19:04
@vercel

vercel Bot commented Aug 14, 2026

Copy link
Copy Markdown

@atikulmunna 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

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/core/pipeline
  pipeline.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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant