Skip to content

test(api): bring the webhook node and extract processor tests under strict pyrefly - #46438

Open
MindOfqc wants to merge 10 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-webhook-node-and-extract-processor
Open

MindOfqc wants to merge 10 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-webhook-node-and-extract-processor

Conversation

@MindOfqc

Copy link
Copy Markdown
Contributor

Why

api/tests/unit_tests/pyrefly.toml keeps the strict-mode debt list. This removes two entries
and pays the debt.

What changed

core/workflow/nodes/trigger_webhook/test_node.py

  • Dropped two assignments to runtime_state.app_config. The runtime state class does not define
    that field anywhere in the repository; the tenant comes from run_context, so the assignment
    was dead code rather than a test hook.
  • Added a module-level _mapping(value: object) -> dict[str, object] helper that narrows the
    default config before indexing, replacing nine untrusted subscripts.
  • Typed the file helper and the parametrized method argument; added -> None to the tests.

core/rag/extractor/test_extract_processor.py

  • The extraction input is now built with UploadFileExtractionInput.model_validate(...), the
    same path core/rag/extractor/extract_processor.py uses, instead of passing a raw upload.
  • Typed the captured-field dict, the extractor doubles and the parametrized arguments.

Verification

  • Strict pyrefly on api/ with both entries removed: the two target files report 0
    diagnostics (was 35 and 36). Total stays at the 29 missing-import entries the local venv
    raises for optional dependencies, WARN is 0.
  • pytest on both files → 78 passed, identical to the untouched baseline.
  • ruff check --no-cache and ruff format --no-cache --check pass.

…trict pyrefly

The webhook node tests dropped two assignments to a runtime-state field the
runtime does not define, and read the default config through a narrowing helper
instead of indexing an object. The extract processor tests build the extraction
input the way the extractor itself does, and type the captured upload fields.
@github-actions github-actions Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label 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-23 19:13:55.621457603 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-23 19:13:28.948250406 +0000
@@ -3878,10 +3878,6 @@
    --> tests/unit_tests/core/rag/datasource/test_retrieval_service.py:148:18
 ERROR Argument `list[FromClause]` is not assignable to parameter `tables` with type `Sequence[Table] | None` in function `sqlalchemy.sql.schema.MetaData.create_all` [bad-argument-type]
   --> tests/unit_tests/core/rag/extractor/test_excel_extractor.py:17:56
-ERROR Argument `UploadFile` is not assignable to parameter `upload_file` with type `Mapping[str, Any] | UploadFileExtractionInput | None` in function `core.rag.extractor.entity.extract_setting.ExtractSetting.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/extractor/test_extract_processor.py:249:25
-ERROR Argument `object | None` is not assignable to parameter `session` with type `Session | None` in function `core.rag.extractor.extract_processor.ExtractProcessor.extract` [bad-argument-type]
-   --> tests/unit_tests/core/rag/extractor/test_extract_processor.py:252:85
 ERROR Method `run` inherited from class `BaseRerankRunner` has no implementation and cannot be accessed via `super()` [abstract-method-call]
    --> tests/unit_tests/core/rag/rerank/test_rerank_model.py:425:16
 ERROR Object of class `FunctionType` has no attribute `return_value` [missing-attribute]
@@ -4733,30 +4729,6 @@
    --> tests/unit_tests/core/workflow/nodes/knowledge_retrieval/test_knowledge_retrieval_node.py:617:16
 ERROR `None` is not subscriptable [unsupported-operation]
    --> tests/unit_tests/core/workflow/nodes/knowledge_retrieval/test_knowledge_retrieval_node.py:618:16
-ERROR Object of class `RuntimeState` has no attribute `app_config` [missing-attribute]
-  --> tests/unit_tests/core/workflow/nodes/trigger_webhook/test_node.py:66:5
-ERROR Object of class `RuntimeState` has no attribute `app_config` [missing-attribute]
-  --> tests/unit_tests/core/workflow/nodes/trigger_webhook/test_node.py:67:5
-ERROR Object of class `RuntimeState` has no attribute `app_config` [missing-attribute]
-   --> tests/unit_tests/core/workflow/nodes/trigger_webhook/test_node.py:485:5
-ERROR Cannot index into `object` [bad-index]
-   --> tests/unit_tests/core/workflow/nodes/trigger_webhook/test_node.py:503:12
-ERROR Cannot index into `object` [bad-index]
-   --> tests/unit_tests/core/workflow/nodes/trigger_webhook/test_node.py:504:12
-ERROR Cannot index into `object` [bad-index]
-   --> tests/unit_tests/core/workflow/nodes/trigger_webhook/test_node.py:505:12
-ERROR Cannot index into `object` [bad-index]
-   --> tests/unit_tests/core/workflow/nodes/trigger_webhook/test_node.py:506:12
-ERROR Cannot index into `object` [bad-index]
-   --> tests/unit_tests/core/workflow/nodes/trigger_webhook/test_node.py:507:12
-ERROR Cannot index into `object` [bad-index]
-   --> tests/unit_tests/core/workflow/nodes/trigger_webhook/test_node.py:508:12
-ERROR Cannot index into `object` [bad-index]
-   --> tests/unit_tests/core/workflow/nodes/trigger_webhook/test_node.py:509:12
-ERROR Cannot index into `object` [bad-index]
-   --> tests/unit_tests/core/workflow/nodes/trigger_webhook/test_node.py:510:12
-ERROR Cannot index into `object` [bad-index]
-   --> tests/unit_tests/core/workflow/nodes/trigger_webhook/test_node.py:511:12
 ERROR Argument `SimpleNamespace` is not assignable to parameter `init_params` with type `InitParams` in function `core.workflow.node_factory.DifyNodeFactory.__init__` [bad-argument-type]
    --> tests/unit_tests/core/workflow/test_node_factory.py:119:29
 ERROR Argument `SimpleNamespace` is not assignable to parameter `runtime_state` with type `RuntimeState` in function `core.workflow.node_factory.DifyNodeFactory.__init__` [bad-argument-type]

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 87.16% 87.20% +0.04%
Strict coverage 86.82% 86.87% +0.04%
Typed symbols 96,578 96,632 +54
Untyped symbols 14,280 14,235 -45
Modules 4652 4652 0

@codecov

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.73418% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.39%. Comparing base (a92a64a) to head (26774ba).

Files with missing lines Patch % Lines
api/core/workflow/nodes/trigger_webhook/node.py 98.73% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #46438   +/-   ##
=======================================
  Coverage   90.39%   90.39%           
=======================================
  Files        6534     6534           
  Lines      409459   409525   +66     
  Branches    82225    82230    +5     
=======================================
+ Hits       370116   370197   +81     
+ Misses      32944    32932   -12     
+ Partials     6399     6396    -3     
Flag Coverage Δ
api 90.15% <98.73%> (+<0.01%) ⬆️

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.

…k-node-and-extract-processor

# Conflicts:
#	api/tests/unit_tests/pyrefly.toml
def test_webhook_node_default_config():
def test_webhook_node_default_config() -> None:
"""Test webhook node default configuration."""
config = TriggerWebhookNode.get_default_config()

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.

can we just type here? or type the origin code.

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.

I typed this at the source: TriggerWebhookNode.get_default_config() now returns WebhookNodeDefaultConfig, including the nested config shape. The test reads config["config"] directly, and the test-only narrowing helper is gone.

@asukaminato0721 asukaminato0721 self-assigned this Nov 15, 2026
`TriggerWebhookNode.get_default_config` returned `Mapping[str, object]`, so
`config["config"]` could only be inferred as `object` and the test needed a
narrowing helper to read the payload it had just built.

Declare the shape instead: two TypedDicts describing the returned mapping and
its `config` section, and narrow the return type. The base class returns
`Mapping[str, object]`, so this is a covariant override; every non-test caller
annotates the result as `Mapping[str, object]` already, and none needs a
change.

The test then drops the helper and asserts the full documented payload rather
than only `method`.

Verified: pyrefly 29 errors / 0 warnings / 0 non-missing-import / 0 for this
file, 114 tests pass, ruff clean.
@MindOfqc

Copy link
Copy Markdown
Contributor Author

Took the second option: typed the origin code.

TriggerWebhookNode.get_default_config returned Mapping[str, object], which is why config["config"] could only be read as object and the test needed a narrowing helper. It now returns two declared TypedDicts. The base class returns Mapping[str, object], so this is a covariant override, and all seven non-test callers already annotate the result as Mapping[str, object] — none needed a change. The test helper is deleted.

…yrefly-webhook-node-and-extract-processor

Resolve the `project-excludes` conflict: upstream graduated
`core/workflow/test_node_runtime.py` in another PR, so that entry stays
excluded, while this branch's own `core/workflow/nodes/trigger_webhook/test_node.py`
stays graduated here.

@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 c97ba68e54, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong.

dify-codereview

@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 1cf09eed92, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.

dify-codereview

@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
Exercise parse_webhook_file rejection paths, the record-id precedence,
and generate_file_var without a usable transfer method or when the file
factory fails, so the patch coverage gate sees the new branches.

@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 9dee343450, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong. 4 review threads are still unresolved.

dify-codereview

@classmethod
@override
def get_default_config(cls, filters: Mapping[str, object] | None = None) -> Mapping[str, object]:
def get_default_config(cls, filters: Mapping[str, object] | None = None) -> WebhookNodeDefaultConfig:

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.

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.

@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
The review asked to model the webhook file shape once at the boundary and
let consumers read typed fields instead of re-checking. parse_webhook_file
now drops any declared field whose value does not match the type the
TypedDict declares for it (unknown keys still travel as JSON scalars via
extra_items), so _webhook_file_record_id and generate_file_var can read
reference/related_id/transfer_method without isinstance re-validation.
WebhookNodeConfig headers/params/body use the real WebhookParameter and
WebhookBodyParameter models from the node entities instead of list[object],
and the default config spells method/content_type from the enums.

@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 8b5fa73051, 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
Replace the loose `object` annotations in the webhook file parser with
precise types, and build the typed value at the boundary instead of
filtering a dict and re-checking it where it is used.

- Declare `WebhookFilePayload = Mapping[Hashable, JsonValue]` as the
  payload shape accepted by the parser and use it for `raw`.
- Make the per-field predicates `TypeGuard`s over `JsonValue`, so the
  parser narrows each value once and never needs `object`.
- Have `_parse_webhook_file_fields` return `WebhookFileDict | None`
  directly, so `parse_webhook_file` no longer builds the typed dict from
  a `dict[str, Any]` via `**kwargs`.
- Type the test fixture payload with `WebhookFilePayload` instead of an
  ad hoc dict.

@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 66d62952b7, 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

JSON scalars) as a ``WebhookFileDict`` when at least one survives, and ``None`` otherwise.
``raw`` is never mutated.
"""
if not isinstance(raw, Mapping):

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.

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.

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 is the parser boundary, not a re-validation of an already trusted value. parse_webhook_file accepts untrusted WebhookFilePayload | Sequence[JsonValue] | None; the Mapping check rejects non-mapping inputs before _parse_webhook_file_fields parses the fields into a new WebhookFileDict. Callers consume that typed result without re-checking its fields, so this is an intentional boundary check.

@asukaminato0721 asukaminato0721 removed the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 17, 2026
@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 17, 2026
# Conflicts:
#	api/tests/unit_tests/core/rag/extractor/test_extract_processor.py

@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 26774baeb0, 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

session: object | None = None,
):
session: Session | None = None,
) -> tuple[str, tuple[object, ...], dict[str, object]]:

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.

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.

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.

Thanks for flagging this. The dict[str, object] here records keyword arguments passed to the monkeypatched extractor constructors. Those constructors don't share a single keyword schema, and the recorded arguments aren't the upload payload. The upload input already uses the production UploadFileExtractionInput.model_validate(...) at the boundary. Is there another production type you had in mind for the recorder?

@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 23, 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 186 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:L This PR changes 100-499 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