test: enable mypy typing checks for test/components/routers/ - #12354
Open
atikulmunna wants to merge 1 commit into
Open
test: enable mypy typing checks for test/components/routers/#12354atikulmunna wants to merge 1 commit into
atikulmunna wants to merge 1 commit into
Conversation
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 is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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]]wherelist[Route]is expected. Annotating those literals clears 26 errors by itself, and becauseRouteis aTypedDictit 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: ignorenaming the specific violation, for exampletypeddict-itemfor a missing key andtypeddict-unknown-keyfor an extra one. Being wrong is what those tests assert, and the error code documents which kind of wrong.Three other things:
test_metadata_routernow asserts the concrete type of routed items before reading.dataor.content. That narrows theDocument | ByteStreamunion and also covers something the tests did not previously check.test_file_routercalledoutput.get(key)and thenlen()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__keeptype: ignore[attr-defined], matching howtest/core/component/test_component.pyandtest/components/generators/chat/test_llm.pyalready handle the attributes the@componentdecorator adds at runtime.How did you test it?
All through hatch, on Python 3.10:
hatch run test:typesgivesSuccess: no issues found in 434 source fileshatch run fmt-checkgivesAll checks passed!hatch run test:unit test/components/routersgives104 passed, 30 skippedpre-commit runover the changed files: all applicable hooks passNotes 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_typeistype | list[type], which does not cover everythingConditionalRouteraccepts.conditional_router.pyhas an explicit branch for "Union types (including Optional and X | Y syntax)", and there are passing tests usingCallable[[int, str], bool]andlist[str] | dict[str, int] | Noneas anoutput_type. Neither is atype, so both need an ignore today.2.
MetadataRouter.runtakeslist[Document] | list[ByteStream], which excludes a mixed list.test_run_with_mixed_documents_and_byte_streamspasses 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
test:.