test: enable mypy typing checks for test/components/preprocessors - #12504
Merged
Conversation
Add test/components/preprocessors/ to the mypy target in pyproject.toml and fix
the type errors that surfaced.
The bulk of the baseline came from one pattern repeated seven times in
test_recursive_splitter.py, where the dict returned by run() was rebound to the
list it contains:
doc_chunks = splitter.run([doc])
doc_chunks = doc_chunks["documents"]
Giving the two values separate names drops that file from 206 errors to 26, and
the directory from 265 to 85.
The rest is Document.content being str | None: narrowing asserts before use,
which check strictly more than before. Generators and comprehensions over
.content are expanded into loops because a narrowing in an enclosing block does
not reach a comprehension's own scope. Six stale type: ignore comments on
read_csv calls in test_csv_document_splitter.py are removed, since
warn_unused_ignores flags them once the directory enters the target. Tests that
pass a wrong type on purpose keep the value and carry a specific ignore code.
No test assertion, expected value, or input datum changed.
ShousenZHANG
requested review from
sjrl
and
a lite review from Copilot
and removed request for
a team
August 29, 2026 07:12
|
@ShousenZHANG is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
38 tasks
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
Claimed in this comment.
Proposed Changes:
test/components/preprocessors/was not in the repository mypy target, so its 13 modules were not checked byhatch run test:types. This adds the path and fixes the errors that surfaced.The baseline was 265 errors, but 206 of them were in
test_recursive_splitter.pyalone, cascading from one pattern repeated seven times where the dict returned byrun()was rebound to the list it contains:Naming the two values separately drops that file from 206 errors to 26, and the directory from 265 to 85.
The remaining 85 are mostly
Document.contentbeingstr | None. AGENTS.md asks to avoidtype: ignore, casts and assertions where possible and to explain them where they are necessary, so:assert ... is not Nonebefore first use, where the value was already being dereferenced.cast(str, ...)would type-check equally well but keeps the old behaviour of failing with aTypeErrorfrom inside a comprehension; the assert names the expectation at the point it is made.castalso appears only twice in all oftest/..contentexpanded into loops. A narrowing in an enclosing block does not reach a comprehension's own scope, even when the comprehension reuses the same name, so the assert has to live inside the iteration. The alternative already merged in this campaign is test: enable mypy typing checks for test/components/samplers/ #12433's in-comprehension filter plus a compensatingassert len(filtered) == len(docs). That is two lines instead of four, but it is only equivalent as long as every one of the 14 sites remembers the length guard; dropping it silently skips elements. I went with the loop because it cannot be got wrong site-by-site — happy to convert to the filter idiom if you prefer the smaller diff.any(...)kept asany(...)over the collected list rather than expanded into per-element asserts, since turning an existential check into a universal one would change what the test claims.Two smaller groups:
# type: ignorecomments onread_csv(...)intest_csv_document_splitter.pyare stale.warn_unused_ignoresflags them as soon as the directory enters the target, so removing them is required rather than optional.[arg-type]/[list-item]), never a bare one.test_document_preprocessor.pyneeded a different fix:Pipeline.get_component()returns theComponentprotocol, so the attribute reads failed. Anisinstancenarrowing resolves it, the same way #12343 and #12319 did.No test assertion, expected value, or input datum changed. Assert counts per file only go up or stay flat.
How did you test it?
hatch run test:types->Success: no issues found in 481 source files(468 before).hatch run test:unit test/components/preprocessors/-> 361 passed, 19 deselected — identical to the counts onmainbefore the change.hatch run test:integration test/components/preprocessors/-> 9 passed, 10 skipped. The 10 skipped are theOPENAI_API_KEY-gated embedding-splitter tests; six of the edits land in those bodies, so they are covered by mypy and collection but were not executed. To check those six anyway I ran their pre- and post-change versions side by side against a stubbed embedder over a range of mutated outputs, and the two behave identically.hatch run fmt-check-> clean across the repo (6787 files).Notes for the reviewer
This and my #12481 both add a directory to the same
types =line, so whichever lands second will conflict there and I will rebase it — no need to sequence them on my account.Nine open PRs add tests to these files (#12284, #12346, #12356, #12373, #12424, #12431, #12434, #12468, #12478). All are pure additions and a three-way merge against each is clean in both orders, so this does not block them. Two of them (#12284, #12478) add a
Document.contentdereference that mypy will start checking once this lands; I will leave them a note so their CI does not go red unexpectedly.No release note: CONTRIBUTING.md scopes the requirement to user-facing changes and notes that tests-only changes can carry
ignore-for-release-notes— happy for you to add the label if you would rather have it explicit.This PR was fully generated with an AI assistant. I have reviewed the changes and run the relevant tests.
Checklist