Skip to content

test(api): bring the index processor and message transformer tests under strict pyrefly - #46442

Open
MindOfqc wants to merge 9 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-index-processor-and-message-transformer
Open

MindOfqc wants to merge 9 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-index-processor-and-message-transformer

Conversation

@MindOfqc

Copy link
Copy Markdown
Contributor

Why

Two more modules still need blanket exclusion from strict checking: the index
processor base tests and the tool invoke message transformer tests. This brings
both under strict mode, so the exclusion list shrinks and regressions in these
areas get caught by the type checker.

What changed

  • api/tests/unit_tests/core/rag/index_processor/test_index_processor_base.py (43 → 0)
    • Annotated every test and stub.
    • The forwarding double cannot call super().method(...) because the inherited
      member is abstract, so it calls BaseIndexProcessor.method(self, ...)
      directly. This keeps the original behaviour of asserting that the base
      implementation raises.
  • api/tests/unit_tests/core/tools/utils/test_message_transformer.py (47 → 0)
    • Annotated every test and stub.
    • Narrowed the ToolInvokeMessage.message union with isinstance assertions
      before touching a concrete variant, and additionally narrowed
      json_object (which is dict | list) before indexing it.
    • Replaced type(self).last_call = ... with a class-level declaration.

No suppression comments were added, and none of the pre-existing typing.cast
calls were extended: the two modules now pass strict checking with real
annotations and explicit narrowing.

Verification

  • Strict check over the whole project, with both entries removed from the
    exclusion list: 29 errors, all of them the pre-existing missing-import
    baseline
    , and zero warnings. The 29 are byte-identical to the baseline
    set (file:line compared one by one).
  • Reverting just the two test files reproduces 90 diagnostics (43 + 47),
    so the fix is what removes them.
  • pytest for both modules: 49 passed before and after.
  • ruff check and ruff format --check: clean.

…der strict pyrefly

Replace the type suppressions these two modules needed with real annotations and
explicit narrowing helpers, so both can leave the strict-mode exclusion list.

- Annotate every test and stub method, using a shared mapping helper instead of
  casting the untrusted JSON payloads.
- Narrow the tool invoke message union with isinstance assertions before touching
  a concrete variant.
- Forward the abstract index processor methods through the base class directly,
  because the super() form resolves to an unimplemented abstract member.
@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-26 22:02:36.024163039 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-26 22:02:24.532102606 +0000
@@ -4055,26 +4055,6 @@
    --> tests/unit_tests/core/tools/utils/dataset_retriever/test_dataset_multi_retriever_tool.py:418:31
 ERROR Argument `_FakeFlaskApp` is not assignable to parameter `flask_app` with type `Flask` in function `core.tools.utils.dataset_retriever.dataset_multi_retriever_tool.DatasetMultiRetrieverTool._retriever` [bad-argument-type]
    --> tests/unit_tests/core/tools/utils/dataset_retriever/test_dataset_multi_retriever_tool.py:469:31
-ERROR `None` is not subscriptable [unsupported-operation]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:155:16
-ERROR Argument `Literal['not a dict']` is not assignable to parameter `d` with type `dict[str, Any]` in function `core.tools.utils.message_transformer.safe_json_dict` [bad-argument-type]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:379:32
-ERROR Argument `list[int]` is not assignable to parameter `d` with type `dict[str, Any]` in function `core.tools.utils.message_transformer.safe_json_dict` [bad-argument-type]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:382:32
-ERROR Argument `Literal[42]` is not assignable to parameter `d` with type `dict[str, Any]` in function `core.tools.utils.message_transformer.safe_json_dict` [bad-argument-type]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:385:32
-ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `json_object` [missing-attribute]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:424:48
-ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `json_object` [missing-attribute]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:461:48
-ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `json_object` [missing-attribute]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:507:24
-ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `json_object` [missing-attribute]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:530:48
-ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `json_object` [missing-attribute]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:551:48
-ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `json_object` [missing-attribute]
-   --> tests/unit_tests/core/tools/utils/test_message_transformer.py:608:48
 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/tools/workflow_as_tool/test_tool.py:78:56
 ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `variable_name` [missing-attribute]

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 87.92% 87.94% +0.02%
Strict coverage 87.58% 87.61% +0.02%
Typed symbols 97,241 97,269 +28
Untyped symbols 13,415 13,389 -26
Modules 4672 4672 0

@codecov

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.36%. Comparing base (f2b8315) to head (d4314a0).
⚠️ Report is 199 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #46442      +/-   ##
==========================================
+ Coverage   90.31%   90.36%   +0.04%     
==========================================
  Files        6495     6512      +17     
  Lines      408426   409804    +1378     
  Branches    81921    82040     +119     
==========================================
+ Hits       368857   370299    +1442     
+ Misses      33163    33109      -54     
+ Partials     6406     6396      -10     
Flag Coverage Δ
api 90.20% <100.00%> (+0.07%) ⬆️

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.

# Conflicts:
#	api/tests/unit_tests/pyrefly.toml
@@ -347,7 +376,7 @@ def test_download_image_validates_size_and_empty_content(self, processor: _Forwa
empty = Mock()

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.

.

@asukaminato0721 asukaminato0721 self-assigned this Nov 15, 2026
@MindOfqc

Copy link
Copy Markdown
Contributor Author

Done. The four responses are now real httpx.Response objects built by an _image_response(...) helper (with the request bound so raise_for_status() behaves as it does in production), so _download_image runs against actual httpx behavior instead of a Mock with hand-written headers/iter_bytes.

The two remaining Mock()s (lines 190/202) are the splitter stand-ins, not responses, and were left alone.

Verified: file goes 0 diagnostics under the strict tests project, 23 tests pass, ruff clean.

@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.

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

dify-codereview

Use a ParamSpec for the forwarding index processor stub, drop the
runtime dict guard in safe_json_dict in favour of a precise Mapping
parameter, and read transformed JSON payloads through pydantic models
instead of ad hoc isinstance assertions.

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

dify-codereview

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 16, 2026
@MindOfqc

Copy link
Copy Markdown
Contributor Author

The Chromium Full (1/3) failure is an infrastructure issue, not a regression from this PR: the job log shows Web build download failed (1). See .../cucumber-web-build.log followed by Process completed with exit code 1 during the web build download step, before any test ran. This PR only touches api/tests/unit_tests/... (no web/ changes), so it cannot affect the frontend E2E build. The API unit/controller/integration shards and Style checks are green. Happy to re-run once the build artifact download recovers.

asukaminato0721

This comment was marked as outdated.

@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
…nsformer

Resolve the conflict in test_index_processor_base.py by keeping this branch's
ParamSpec forwarding overrides and the _image_response helper (real httpx
responses always carry a request so raise_for_status cannot silently fail),
and dropping the now-unused imports the upstream variant needed.

@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 3b95dd0f5f, 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 16, 2026
@MindOfqc

Copy link
Copy Markdown
Contributor Author

Update on the latest Chromium Full (3/3) failure: it is a flaky web E2E, distinct from the earlier Web build download failed infrastructure issue. The only failed scenario (Ignore an external redirect target for an authenticated user) times out on getByRole('heading', { name: 'Templates', exact: true }) (5000ms) — the identical element that flaked in #46393's Chromium Full (2/3). Sibling shards passed, this PR changes only api/core/tools/utils/message_transformer.py + tests (zero web/ files), and main HEAD's Web E2E is green. Intermittent timing flake in the shared suite; a re-run should clear it.

asukaminato0721

This comment was marked as outdated.

@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
…d-message-transformer

Take upstream parametrised MIME test, which supersedes the branch copy and covers both None and empty values.
@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 22, 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 10aab35e50, 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



def safe_json_dict(d: dict[str, Any]):
def safe_json_dict(d: object) -> dict[object, 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.

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.

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.

The broad type matches the existing runtime contract here. This parameter is the validation boundary: the helper accepts the value and explicitly rejects non-dicts with TypeError. It also preserves non-string keys and passes unsupported nested values through, so a JsonValue return type would promise more than it does. The tests now cover both behaviours.

@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 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 27, 2026
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