Repository navigation
test(api): bring the knowledge index node tests under strict pyrefly - #46483
shuxiachai wants to merge 3 commits into
Conversation
Remove test_knowledge_index_node.py and the stale events/test___init__.py entry from the unit-test pyrefly project-excludes. The patching fixtures now return the mocks created by mocker.patch.object, and the summary setting in the pass-through test uses the SummaryIndexSettingDict key `enable`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-11-16 05:09:42.591307813 +0000
+++ /tmp/pyrefly_pr.txt 2026-11-16 05:09:25.351102374 +0000
@@ -4857,8 +4857,6 @@
--> tests/unit_tests/core/workflow/nodes/human_input/test_entities.py:1456:29
ERROR Class member `_EventQueue.listen` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
--> tests/unit_tests/core/workflow/nodes/human_input/test_entities.py:1484:9
-ERROR Argument `dict[str, bool]` is not assignable to parameter `summary_index_setting` with type `SummaryIndexSettingDict | None` in function `core.workflow.nodes.knowledge_index.knowledge_index_node.KnowledgeIndexNode._invoke_knowledge_index` [bad-argument-type]
- --> tests/unit_tests/core/workflow/nodes/knowledge_index/test_knowledge_index_node.py:638:35
ERROR `None` is not subscriptable [unsupported-operation]
--> tests/unit_tests/core/workflow/nodes/knowledge_retrieval/test_knowledge_retrieval_node.py:615:16
ERROR `None` is not subscriptable [unsupported-operation]
|
Pyrefly Type Coverage
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #46483 +/- ##
==========================================
- Coverage 90.41% 90.34% -0.07%
==========================================
Files 6334 6363 +29
Lines 396678 402436 +5758
Branches 79712 80626 +914
==========================================
+ Hits 358642 363571 +4929
- Misses 31928 32459 +531
- Partials 6108 6406 +298
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
1 inline suggestion at 9c8a2372a1, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong.
dify-codereview
…tching KnowledgeIndexNode now takes its index processor and summary service as optional keyword arguments typed by the existing protocols, as KnowledgeRetrievalV2Node does with its services. Without them it still builds IndexProcessor and SummaryIndex, so the node factory is unchanged. Align summary_index_setting in both protocols with SummaryIndexSettingDict, the type IndexProcessor and SummaryIndex accept; nothing used the protocols before. The tests pass recording implementations of both protocols and no longer patch the node module. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
asukaminato0721
left a comment
There was a problem hiding this comment.
1 inline suggestion at 68e0bd3070, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong.
dify-codereview
| dataset_id: str | ||
| document_id: str | ||
| original_document_id: str | ||
| chunks: Mapping[str, object] |
There was a problem hiding this comment.
Parse, don't validate (https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/): model this data once at the boundary as a pydantic model / dataclass / TypedDict (Model.model_validate(raw)), and pass the typed value on, instead of passing dicts around and re-checking keys and types where they are used. In a test, don't build this as an ad hoc dict[str, object]: define the shape (TypedDict / model) in the production code and import it here.
dify-codereview · parse-dont-validate Learned from an earlier review. Reply here if this is wrong.
There was a problem hiding this comment.
Thanks. The sample chunks are now built from MultimodalGeneralStructureChunk / GeneralChunk in core/rag/models/document.py, the model ParagraphIndexProcessor validates them with, and the node data uses IndexStructureType.PARAGRAPH_INDEX instead of the unsupported "general_structure" (5b9720c).
IndexCall.chunks itself stays Mapping[str, object] on purpose: it records the argument exactly as the node forwarded it, and IndexProcessorProtocol.index_and_clean types that argument as Mapping[str, Any]. The node does not parse chunks. Their model depends on the chunk structure, and each index processor parses them at its own boundary (MultimodalGeneralStructureChunk, ParentChildStructureChunk, QAStructureChunk). Moving that parsing into the node would change production behaviour, which is outside the scope of this PR, so I've left that boundary as it is.
The sample chunks were ad hoc dicts, and the node data named a chunk
structure ("general_structure") that IndexProcessorFactory does not
support. Build the chunks from MultimodalGeneralStructureChunk, the
model the paragraph index processor validates them with, and use
IndexStructureType.PARAGRAPH_INDEX.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
asukaminato0721
left a comment
There was a problem hiding this comment.
1 inline suggestion at 5b9720ca7c, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong. 1 review thread is still unresolved.
dify-codereview
| document_id: str | ||
| original_document_id: str | ||
| chunks: Mapping[str, object] | ||
| batch: object |
There was a problem hiding this comment.
Please use the precise type here, not object / Any: e.g. -> str, -> Session, Callable[...]; for a parametrized test, the union of the case types (or a type alias for it); for __exit__, exc_type: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None.
dify-codereview · precise-types (concrete-type) Learned from an earlier review. Reply here if this is wrong.
Summary
#46482
core/workflow/nodes/knowledge_index/test_knowledge_index_node.pyleavesproject-excludesinapi/tests/unit_tests/pyrefly.toml. Theevents/test___init__.pyentry is dropped too: that file already passes strict checking.project-excludes: 154 → 152.mock_index_processorandmock_summary_index_serviceused to return the realIndexProcessor/SummaryIndexinstances, and the tests then reached into.index_and_clean.return_valueand similar. Typed as the real classes, those attributes are bound methods, not mocks. The fixtures now return small frozen dataclasses that hold the mocksmocker.patch.objectreturned. The test bodies keep the same attribute paths and get typed access.test_invoke_with_summary_index_settingpassed{"enabled": True}assummary_index_setting, butSummaryIndexSettingDictdefinesenable. It is now a typedSummaryIndexSettingDictwith the correct key. The node forwards the setting unchanged, so the assertions are the same._build_nodeand the test methods.No
# pyrefly: ignore, notyping.cast, and no newMock()/MagicMock()constructions.Verification
uv run --directory api --dev pyrefly check --summary=none --use-ignore-files=false --disable-project-excludes-heuristics=true --config=tests/unit_tests/pyrefly.toml→ no errors or warnings./dev/pyrefly-check-local→ exit 0pytest api/tests/unit_tests/core/workflow/nodes/knowledge_index/test_knowledge_index_node.py api/tests/unit_tests/events/test___init__.py→ 13 passedmake lintsteps (ruff format and check, response contract lint, import linter, dotenv-linter) → passUpdate after review
Following the review,
KnowledgeIndexNodetakesindex_processorandsummary_index_serviceas optional keyword arguments typed byIndexProcessorProtocolandSummaryIndexServiceProtocol; without them it buildsIndexProcessorandSummaryIndex, so the node factory is unchanged. The protocols'summary_index_settingnow matches theSummaryIndexSettingDict | Nonethat both implementations accept. The tests pass recording implementations of the protocols instead of patching the node module, so the fixture dataclasses that held the patch mocks are gone.pyrefly(unit-test config) and./dev/pyrefly-check-local→ no errors;pyteston the node, node factory and index processor tests → 99 passed (one existing index processor test needsos.register_at_forkand errors on Windows only);make lintsteps and the staged guards → pass.Screenshots
N/A (backend change)
Checklist
make lint && make type-check(backend) andvp staged(frontend) to appease the lint gods🤖 Generated with Claude Code
From Claude Code