Skip to content

test(api): bring the plugin datasource tests under strict pyrefly - #46353

Open
MindOfqc wants to merge 18 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-plugin-datasource
Open

MindOfqc wants to merge 18 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-plugin-datasource

Conversation

@MindOfqc

Copy link
Copy Markdown
Contributor

test(api): bring the plugin datasource tests under strict pyrefly

Annotate the test functions and model the request payloads so
core/plugin/impl/test_datasource.py can leave the strict-mode exclusion list.

The three fake_request helpers are consumed as mocker.patch.object(..., side_effect=fake_request), so their annotation describes what the patched
callable returns rather than a tuple. Their **kwargs are declared with
Unpack[TypedDict] because a plain object value would make
kwargs["transformer"] uncallable, and the nested payload literals are typed
with module-level TypedDicts because dict invariance otherwise rejects them.
No test logic, assertion, or expected value is touched.

Verification:

  • project-wide pyrefly check on tests/unit_tests/pyrefly.toml: 29 errors,
    all missing-import from packages absent in the local venv, zero warnings
    (min-severity = "warn", so a warning alone fails make type-check-core).
  • target file reports zero diagnostics.
  • pytest on the file: 11 passed before and after.
  • ruff check and ruff format --check pass.

Annotate the test functions and model the request payloads so
`core/plugin/impl/test_datasource.py` can leave the strict-mode exclusion list.

The three `fake_request` helpers are consumed as `mocker.patch.object(...,
side_effect=fake_request)`, so their annotation describes what the patched
callable returns rather than a tuple. Their `**kwargs` are declared with
`Unpack[TypedDict]` because a plain `object` value would make
`kwargs["transformer"]` uncallable, and the nested payload literals are typed
with module-level `TypedDict`s because dict invariance otherwise rejects them.
No test logic, assertion, or expected value is touched.

Verification:
- project-wide `pyrefly check` on `tests/unit_tests/pyrefly.toml`: 29 errors,
  all `missing-import` from packages absent in the local venv, zero warnings
  (`min-severity = "warn"`, so a warning alone fails `make type-check-core`).
- target file reports zero diagnostics.
- `pytest` on the file: 11 passed before and after.
- `ruff check` and `ruff format --check` pass.
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 14, 2026
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 87.84% 87.85% +0.01%
Strict coverage 87.50% 87.52% +0.01%
Typed symbols 96,991 97,028 +37
Untyped symbols 13,477 13,466 -11
Modules 4667 4667 0

@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.67442% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.33%. Comparing base (4752717) to head (5cfabd5).
⚠️ Report is 153 commits behind head on main.

Files with missing lines Patch % Lines
api/core/plugin/impl/datasource.py 97.43% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #46353      +/-   ##
==========================================
+ Coverage   90.30%   90.33%   +0.02%     
==========================================
  Files        6493     6504      +11     
  Lines      408308   409465    +1157     
  Branches    81880    81949      +69     
==========================================
+ Hits       368728   369881    +1153     
- Misses      33176    33200      +24     
+ Partials     6404     6384      -20     
Flag Coverage Δ
api 90.17% <97.67%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asukaminato0721 asukaminato0721 self-assigned this Nov 15, 2026
@github-actions github-actions Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Nov 15, 2026
@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-11-25 11:39:57.955940251 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-25 11:39:47.663854368 +0000
@@ -1147,27 +1147,27 @@
 ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `SQLCoreOperations[TenantStatus] | TenantStatus` in function `models.account.Tenant.__init__` [bad-argument-type]
   --> tests/e2e_tests/containers/services/tools/test_mcp_tools_manage_service.py:72:20
 ERROR Argument `list[ToolParameter] | None` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
-   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:752:20
+   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:765:20
 ERROR Type `None` is not iterable [not-iterable]
-   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:753:32
+   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:766:32
 ERROR Type `None` is not iterable [not-iterable]
-   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:754:32
+   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:767:32
 ERROR Argument `list[ToolParameter] | None` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
-   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:763:20
+   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:776:20
 ERROR Type `None` is not iterable [not-iterable]
-   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:764:34
+   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:777:34
 ERROR Argument `list[ToolParameter] | None` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
-   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:778:20
+   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:791:20
 ERROR `None` is not subscriptable [unsupported-operation]
-   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:779:16
+   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:792:16
 ERROR Argument `list[ToolParameter] | None` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
-   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:787:20
+   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:800:20
 ERROR Argument `list[ToolParameter] | None` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
-   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:795:20
+   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:808:20
 ERROR Type `None` is not iterable [not-iterable]
-   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:804:33
+   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:817:33
 ERROR `None` is not subscriptable [unsupported-operation]
-   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:805:16
+   --> tests/e2e_tests/containers/services/tools/test_tools_transform_service.py:818:16
 ERROR Object of class `NoneType` has no attribute `id` [missing-attribute]
    --> tests/e2e_tests/containers/services/tools/test_workflow_tools_manage_service.py:109:38
 ERROR Object of class `NoneType` has no attribute `id` [missing-attribute]

The transformer closures are declared by BasePluginClient as
Callable[[dict[str, Any]], dict[str, Any]], so narrowing them to a TypedDict
violated contravariance and brought the payload type reports back onto the
production file the PR was meant to clean up.

Keep the transformer signature as dict[str, Any] and cast the payload inside,
and widen the two fields the daemon really can send empty: the provider name
may be missing, and the schema may be any resolved SchemaType.
`check_no_new_cast.py` rejected the three `cast(PluginDatasource*Payload,
json_response)` calls this branch added to the product code, so the
Style Check job went red.

A `TypedDict` accepts a keyword-splat construction from an
already-`dict[str, Any]` value, which gives the same narrowing without a
`typing.cast` and keeps pyrefly at zero errors on the file.

@asukaminato0721 asukaminato0721 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 inline suggestions at cca150a59b, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.

dify-codereview

Comment thread api/core/plugin/impl/datasource.py Resolved
Comment thread api/core/plugin/impl/datasource.py Resolved

@asukaminato0721 asukaminato0721 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 inline suggestions at cca150a59b, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.

dify-codereview

Comment thread api/core/plugin/impl/datasource.py Resolved
Comment thread api/core/plugin/impl/datasource.py Resolved

@asukaminato0721 asukaminato0721 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 inline suggestions at 9d8e7713fd, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.

dify-codereview

Comment thread api/core/plugin/impl/datasource.py Resolved
Comment thread api/core/plugin/impl/datasource.py Resolved
@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 16, 2026
The typed transformers added branches for payloads without a declaration,
without an identity, and without an output schema. Exercise each fallback
through the real request path so the patch coverage gate sees them.

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is failing: API Tests, Run API Tests / API Unit Tests (3.13, shard 2/2). Please fix it. 2 inline suggestions at 7ae7ea1787, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong. 2 review threads are still unresolved.

dify-codereview

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 16, 2026
@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 16, 2026
Replace the TypedDict labels around the plugin daemon datasource
payloads with Pydantic v2 models so the response shape is parsed, not
just annotated, at the boundary (parse-don't-validate). Declared fields
(identity.name, identity.provider, output_schema) are type-checked on
validation; undeclared keys are carried through with extra="allow"
because the downstream entity models still own the rest of the daemon
payload. The transformers return model_dump(exclude_unset=True) so the
base client re-validates the envelope as before.

The unit tests annotate the transformer payloads as plain dicts and
need the same TID251 per-file ignore for typing.Any that test_plugin.py
already has in tests/unit_tests/.ruff.toml.

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 inline suggestion at 6b0a579cff, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong. 2 review threads are still unresolved.

dify-codereview

@asukaminato0721 asukaminato0721 removed the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 16, 2026
@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 18, 2026
@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 18, 2026
@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 18, 2026
@github-actions github-actions Bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Nov 19, 2026

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 inline suggestion at 542b97b8f4, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong. 3 review threads are still unresolved.

dify-codereview

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 19, 2026

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is failing: Style Check / Python Style. Please fix it. 1 inline suggestion at c1ea9c35b2, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong. 3 review threads are still unresolved.

dify-codereview

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 19, 2026
asukaminato0721

This comment was marked as outdated.

@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 19, 2026

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 inline suggestion at ccd58fc7ed, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong. 3 review threads are still unresolved.

dify-codereview

return httpx.Response(200, json=daemon_response)

with httpx.Client(transport=httpx.MockTransport(handle)) as http_client:
mocker.patch.object(plugin_client_module, "_httpx_client", http_client)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an external boundary: define a small typing.Protocol for what the code needs, have the production code accept it as a parameter, and pass a fake that implements the Protocol in the test, instead of patching or mocking it.

dify-codereview · tests-real-dependencies (protocol-di) Learned from an earlier review. Reply here if this is wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test swaps the module-level HTTP client for a real httpx.Client backed by MockTransport. It still runs the production request builder and response parser, and checks the actual GET to /management/datasource, the provider and plugin_id query parameters, and the normalized response. The injected DatasourceProviderListRequester serves /management/datasources (plural), and the neighboring tests exercise both list entry points with it. I kept the single-provider test at the HTTP transport boundary so it still catches regressions in the default request path without making a network call.

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 21, 2026
MindOfqc and others added 2 commits November 25, 2026 19:04
Inject a narrow requester Protocol for online-drive downloads so tests can use a typed fake instead of patching the private stream helper. Preserve the production transport fallback and lazy yield-from behavior.
@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 25, 2026

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider splitting this into smaller pull requests: it adds 261 lines of non-test code, past about 150. Split it by feature: each pull request carries one functional change together with its tests, so each feature stands on its own and can be reviewed, merged and reverted independently. A rule of thumb, not a requirement. Size counts added non-test lines; tests, lock files and snapshots do not count.

dify-codereview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL This PR changes 500-999 lines, ignoring generated files. waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants