test(api): bring the knowledge fs contract and workflow mock factory under strict pyrefly #46418
Pyrefly Diff
<details>
<summary>base → PR</summary>
--- /tmp/pyrefly_base.txt 2026-11-30 12:53:07.163846374 +0000
+++ /tmp/pyrefly_pr.txt 2026-11-30 12:52:55.483783718 +0000
@@ -1574,12 +1574,6 @@
--> tests/helpers/legacy_model_type_migration.py:139:23
ERROR Object of class `FromClause` has no attribute `insert` [missing-attribute]
--> tests/helpers/legacy_model_type_migration.py:183:13
-ERROR Cannot extend final class `DifyNodeFactory` [invalid-inheritance]
- --> tests/helpers/workflow/mock_factory.py:38:23
-ERROR Class member `MockNodeFactory.create_node` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
- --> tests/helpers/workflow/mock_factory.py:113:9
-ERROR Cannot set item in `dict[str, type[MockAgentNode] | type[MockCodeNode] | type[MockDocumentExtractorNode] | type[MockHttpRequestNode] | type[MockIterationNode] | type[MockKnowledgeRetrievalNode] | type[MockLLMNode] | type[MockLoopNode] | type[MockParameterExtractorNode] | type[MockQuestionClassifierNode] | type[MockTemplateTransformNode] | type[MockToolNode]]` [unsupported-operation]
- --> tests/helpers/workflow/mock_factory.py:205:44
ERROR Unexpected keyword argument `node_id` in function `object.__init__` [unexpected-keyword]
--> tests/helpers/workflow/mock_nodes.py:118:13
ERROR Unexpected keyword argument `data` in function `object.__init__` [unexpected-keyword]
@@ -4117,18 +4111,6 @@
--> tests/unit_tests/core/workflow/test_workflow_entry.py:1795:24
ERROR Object of class `FunctionType` has no attribute `call_args` [missing-attribute]
--> tests/unit_tests/core/workflow/test_workflow_entry.py:1796:35
-ERROR Object of class `object` has no attribute `append` [missing-attribute]
- --> tests/unit_tests/dev/test_generate_knowledge_fs_contract.py:121:13
-ERROR Cannot set item in `object` [unsupported-operation]
- --> tests/unit_tests/dev/test_generate_knowledge_fs_contract.py:130:13
-ERROR Object of class `object` has no attribute `pop` [missing-attribute]
- --> tests/unit_tests/dev/test_generate_knowledge_fs_contract.py:130:32
-ERROR Cannot index into `object` [bad-index]
- --> tests/unit_tests/dev/test_generate_knowledge_fs_contract.py:224:30
-ERROR Cannot index into `object` [bad-index]
- --> tests/unit_tests/dev/test_generate_knowledge_fs_contract.py:232:5
-ERROR `object` is not assignable to dict key `security` with type `dict[str, dict[str, dict[str, object]]]` [bad-assignment]
- --> tests/unit_tests/dev/test_generate_knowledge_fs_contract.py:660:32
ERROR Object of class `NoneType` has no attribute `partition` [missing-attribute]
--> tests/unit_tests/extensions/logstore/repositories/test_logstore_api_workflow_node_execution_repository.py:58:34
ERROR `<=` is not supported between `None` and `int` [unsupported-operation]
</details>
Pyrefly Type Coverage
| Metric | Base | PR | Delta |
| --- | ---: | ---: | ---: |
| Type coverage | 90.82% | 90.82% | +0.00% |
| Strict coverage | 90.47% | 90.47% | +0.00% |
| Typed symbols | 106,067 | 106,073 | +6 |
| Untyped symbols | 10,764 | 10,764 | 0 |
| Modules | 4974 | 4974 | 0 |
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 90.54%. Comparing base (
5d50d5f) to head (912d042).:warning: Report is 1 commits behind head on main.
<details><summary>Additional details and impacted files</summary>
@@ Coverage Diff @@
## main #46418 +/- ##
=======================================
Coverage 90.54% 90.54%
=======================================
Files 6897 6897
Lines 432401 432432 +31
Branches 86698 86699 +1
=======================================
+ Hits 391507 391537 +30
- Misses 34069 34070 +1
Partials 6825 6825
| Flag | Coverage Δ | |
|---|---|---|
| api | 90.08% <ø> (+<0.01%) | :arrow_up: |
Flags with carried forward coverage won't be shown. Click here to find out more.
</details>
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
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.
<sub>dify-codereview · parse-dont-validate Learned from an earlier review. Reply here if this is wrong.</sub>
<!-- dify-review:rule=parse-dont-validate;key=3445c77111533754 -->
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.
<sub>dify-codereview · parse-dont-validate Learned from an earlier review. Reply here if this is wrong.</sub>
<!-- dify-review:rule=parse-dont-validate;key=db1d702898c3369c -->
2 inline suggestions at 1c51058b58, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.
<sub>dify-codereview</sub>
<!-- dify-review:head=1c51058b58ae05f99e5a1a0c84865f384232d734 -->
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.
<sub>dify-codereview · parse-dont-validate Learned from an earlier review. Reply here if this is wrong.</sub>
<!-- dify-review:rule=parse-dont-validate;key=77fec5c90ef0a413 -->
2 inline suggestions at 1c51058b58, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.
<sub>dify-codereview</sub>
<!-- dify-review:head=1c51058b58ae05f99e5a1a0c84865f384232d734 -->
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.
<sub>dify-codereview · parse-dont-validate Learned from an earlier review. Reply here if this is wrong.</sub>
<!-- dify-review:rule=parse-dont-validate;key=81c6a19f34654e6d -->
Updated in fb9b084f. The ready-manifest fixture and its helper now use the production ProductOperationManifest TypedDict, with typed operation entries. The tests can remove an operation and change a method without narrowing dictionaries by hand. The fixture still goes through the production parser, and the two cases still assert the specific completeness and method-drift errors. All 38 generator tests passed and scoped strict Pyrefly reported zero diagnostics.
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.
<sub>dify-codereview · parse-dont-validate Learned from an earlier review. Reply here if this is wrong.</sub>
<!-- dify-review:rule=parse-dont-validate;key=e164b0c440531745 -->
1 inline suggestion at fb9b084fac, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong. 3 review threads are still unresolved.
<sub>dify-codereview</sub>
<!-- dify-review:head=fb9b084face1f83dadb3a41540c07e9992fe425a -->
This is a false positive: MockNodeFactory.with_graph_config is an @override matching the production DifyNodeFactory.with_graph_config(self, graph_config: Mapping[str, Any]) signature (api/core/workflow/node_factory.py:456). It does not construct an ad-hoc dict or inspect keys here—it forwards graph_config directly to self._delegate.with_graph_config(graph_config). Changing the parameter type in the test override would violate Liskov substitution against the production NodeFactory protocol.
I kept this fixture at the raw JSON boundary. The response mutation changes only the exported POST status from 200 to 201, writes the JSON to disk, and checks that the real CLI reports openapiSha256 drift. Production loads that same artifact with load_json_object; this case is about the lock detecting a file change, not about whether the response matches a separate OpenAPI model.
I kept this input deliberately minimal. The extra HEAD /internal-maintenance has no operationId and its 200 response is empty, so it does not meet the full OpenAPI Response Object requirements. The test checks that validate_declarations ignores the unrelated operation instead of walking its response. Pre-validating the whole fixture would reject that input before the declaration checker gets to exercise this behavior.
I kept the media and header descriptor values empty here so the assertion is about the fields the real declaration checker reads: the application/json content key and the X-Trace-Id response header, alongside the request header. validate_declarations consumes the raw parsed document; a full descriptor model would add constraints unrelated to this matching test. The separate product manifest fixture now uses its production TypedDict.
This branch has conflicts with main: please rebase onto the latest main. Checked fb9b084fac.
<sub>dify-codereview</sub>
<!-- dify-review:head=fb9b084face1f83dadb3a41540c07e9992fe425a;conflict=fb9b084face1f83dadb3a41540c07e9992fe425a -->
asukaminato0721
MindOfqc
Why
api/tests/unit_tests/pyrefly.tomlstill listsdev/test_generate_knowledge_fs_contract.pyand the shared../helpers/workflow/mock_factory.pyas strict-mode debt. Neither needs a type suppression, so they should not stay on the list.What changed
mock_factory.pybuilds the node and variable fixtures the workflow contract tests are written against, so the annotations follow the production entities instead of widening withobject:Nodeand acceptNodeDatasubclasses, which is what the call sites already passVariableBase, matchingfactories/variable_factory.pydict[str, object]convention from the other strict-clean helpers undertests/helpers/test_generate_knowledge_fs_contract.py:_mappinghelper before indexing, instead of relying on implicitAnyapi/tests/unit_tests/pyrefly.tomldrops the two entries (199 -> 197).How to test
Locally, with both entries removed from
project-excludes:missing-importerrors, zero warnings, zero non-missing-importerrors, zero remaining diagnostics for either filetest_contract_cli_updates_checks_and_detects_openapi_driftalso fails on the unmodified checkout)test_workflow_entry.py, the other consumer of the shared helper: 77 passedNo new
pyrefly: ignore,type: ignore,castortyping.Any.