Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor(api): enable strict typing for extraction/fields test files (#44821)

Felix Shic65a0ce8fca0
diff --git a/api/tests/unit_tests/core/app/apps/test_message_based_app_queue_manager.py b/api/tests/unit_tests/core/app/apps/test_message_based_app_queue_manager.py
index 90240e9b567..a421aac081f 100644
--- a/api/tests/unit_tests/core/app/apps/test_message_based_app_queue_manager.py
+++ b/api/tests/unit_tests/core/app/apps/test_message_based_app_queue_manager.py
@@ -17,7 +17,7 @@ from core.app.entities.queue_entities import (
class TestMessageBasedAppQueueManager:
- def test_publish_stops_on_terminal_events(self):
+ def test_publish_stops_on_terminal_events(self) -> None:
with patch("core.app.apps.base_app_queue_manager.redis_client") as mock_redis:
mock_redis.setex.return_value = True
manager = MessageBasedAppQueueManager(
@@ -35,7 +35,7 @@ class TestMessageBasedAppQueueManager:
manager._publish(QueueStopEvent(stopped_by=QueueStopEvent.StopBy.USER_MANUAL), Mock())
manager.stop_listen.assert_called_once()
- def test_publish_raises_when_stopped(self):
+ def test_publish_raises_when_stopped(self) -> None:
with patch("core.app.apps.base_app_queue_manager.redis_client") as mock_redis:
mock_redis.setex.return_value = True
manager = MessageBasedAppQueueManager(
@@ -52,7 +52,7 @@ class TestMessageBasedAppQueueManager:
with pytest.raises(GenerateTaskStoppedError):
manager._publish(QueueErrorEvent(error=ValueError("boom")), PublishFrom.APPLICATION_MANAGER)
- def test_publish_enqueues_message_end(self):
+ def test_publish_enqueues_message_end(self) -> None:
with patch("core.app.apps.base_app_queue_manager.redis_client") as mock_redis:
mock_redis.setex.return_value = True
manager = MessageBasedAppQueueManager(
@@ -71,7 +71,7 @@ class TestMessageBasedAppQueueManager:
assert manager._q.qsize() == 1
- def test_publish_pause_event_marks_listener_as_paused(self):
+ def test_publish_pause_event_marks_listener_as_paused(self) -> None:
with patch("core.app.apps.base_app_queue_manager.redis_client") as mock_redis:
mock_redis.setex.return_value = True
manager = MessageBasedAppQueueManager(
@@ -89,7 +89,7 @@ class TestMessageBasedAppQueueManager:
manager.stop_listen.assert_called_once_with(execution_state=AppExecutionState.PAUSED)
- def test_pause_state_is_preserved_when_advanced_chat_message_ends(self):
+ def test_pause_state_is_preserved_when_advanced_chat_message_ends(self) -> None:
with (
patch("core.app.apps.base_app_queue_manager.redis_client") as queue_redis,
patch("core.app.apps.execution_coordinator.redis_client") as execution_redis,
diff --git a/api/tests/unit_tests/core/external_data_tool/test_external_data_fetch.py b/api/tests/unit_tests/core/external_data_tool/test_external_data_fetch.py
index c5b0e52060f..c43eb788279 100644
--- a/api/tests/unit_tests/core/external_data_tool/test_external_data_fetch.py
+++ b/api/tests/unit_tests/core/external_data_tool/test_external_data_fetch.py
@@ -9,11 +9,11 @@ from core.external_data_tool.external_data_fetch import ExternalDataFetch
class TestExternalDataFetch:
@pytest.fixture
- def app(self):
+ def app(self) -> Flask:
app = Flask(__name__)
return app
- def test_fetch_success(self, app: Flask):
+ def test_fetch_success(self, app: Flask) -> None:
with app.app_context():
fetcher = ExternalDataFetch()
@@ -30,7 +30,7 @@ class TestExternalDataFetch:
# This approach is robust regardless of thread scheduling order
from unittest.mock import MagicMock
- def factory_side_effect[**P](*args: P.args, **kwargs: P.kwargs):
+ def factory_side_effect[**P](*args: P.args, **kwargs: P.kwargs) -> None:
variable = kwargs.get("variable")
mock_instance = MagicMock()
if variable == "var1":
@@ -64,7 +64,7 @@ class TestExternalDataFetch:
name="type2", tenant_id="tenant1", app_id="app1", variable="var2", config={"c2": "v2"}
)
- def test_fetch_no_tools(self):
+ def test_fetch_no_tools(self) -> None:
# We don't necessarily need app_context if there are no tools,
# but fetch calls current_app._get_current_object() only inside the loop.
# Wait, let's look at the code.
@@ -79,7 +79,7 @@ class TestExternalDataFetch:
assert result_inputs == inputs
assert result_inputs is not inputs # Should be a copy
- def test_fetch_with_none_variable(self, app: Flask):
+ def test_fetch_with_none_variable(self, app: Flask) -> None:
with app.app_context():
fetcher = ExternalDataFetch()
tool = ExternalDataVariableEntity(variable="var1", type="type1", config={})
@@ -95,7 +95,7 @@ class TestExternalDataFetch:
assert "var1" not in result_inputs
assert result_inputs == {"in": "val"}
- def test_query_external_data_tool(self, app: Flask):
+ def test_query_external_data_tool(self, app: Flask) -> None:
fetcher = ExternalDataFetch()
tool = ExternalDataVariableEntity(variable="var1", type="type1", config={"k": "v"})
diff --git a/api/tests/unit_tests/core/rag/extractor/test_csv_extractor.py b/api/tests/unit_tests/core/rag/extractor/test_csv_extractor.py
index 9809e0f6814..023c28d5d14 100644
--- a/api/tests/unit_tests/core/rag/extractor/test_csv_extractor.py
+++ b/api/tests/unit_tests/core/rag/extractor/test_csv_extractor.py
@@ -1,8 +1,8 @@
import csv
import io
from pathlib import Path
-from types import SimpleNamespace
-from typing import override
+from types import SimpleNamespace, TracebackType
+from typing import Self, override
import pandas as pd
import pytest
@@ -13,11 +13,16 @@ from core.rag.extractor.csv_extractor import CSVExtractor
class _ManagedStringIO(io.StringIO):
@override
- def __enter__(self):
+ def __enter__(self) -> Self:
return self
@override
- def __exit__(self, exc_type, exc_val, exc_tb):
+ def __exit__(
+ self,
+ exc_type: type[BaseException] | None,
+ exc_val: BaseException | None,
+ exc_tb: TracebackType | None,
+ ) -> None:
self.close()
@@ -43,7 +48,7 @@ class TestCSVExtractor:
assert docs[0].page_content == "value: 123.0;body: nan"
assert csv_args == {"sep": ";", "dtype": {"value": int}, "keep_default_na": True}
- def test_extract_success_with_source_column(self, tmp_path: Path):
+ def test_extract_success_with_source_column(self, tmp_path: Path) -> None:
file_path = tmp_path / "data.csv"
file_path.write_text("id,body\nsource-1,hello\n", encoding="utf-8")
@@ -54,7 +59,7 @@ class TestCSVExtractor:
assert docs[0].page_content == "id: source-1;body: hello"
assert docs[0].metadata == {"source": "source-1", "row": 0}
- def test_extract_raises_when_source_column_missing(self, tmp_path: Path):
+ def test_extract_raises_when_source_column_missing(self, tmp_path: Path) -> None:
file_path = tmp_path / "data.csv"
file_path.write_text("id,body\nsource-1,hello\n", encoding="utf-8")
@@ -63,10 +68,10 @@ class TestCSVExtractor:
with pytest.raises(ValueError, match="Source column 'missing_col' not found"):
extractor.extract()
- def test_extract_wraps_unicode_error_when_autodetect_disabled(self, monkeypatch: pytest.MonkeyPatch):
+ def test_extract_wraps_unicode_error_when_autodetect_disabled(self, monkeypatch: pytest.MonkeyPatch) -> None:
extractor = CSVExtractor("dummy.csv", autodetect_encoding=False)
- def raise_decode[**P](*args: P.args, **kwargs: P.kwargs):
+ def raise_decode[**P](*args: P.args, **kwargs: P.kwargs) -> None:
raise UnicodeDecodeError("utf-8", b"x", 0, 1, "decode error")
monkeypatch.setattr("builtins.open", raise_decode)
@@ -74,11 +79,15 @@ class TestCSVExtractor:
with pytest.raises(RuntimeError, match="Error loading dummy.csv"):
extractor.extract()
- def test_extract_autodetect_encoding_success(self, monkeypatch: pytest.MonkeyPatch):
+ def test_extract_autodetect_encoding_success(self, monkeypatch: pytest.MonkeyPatch) -> None:
extractor = CSVExtractor("dummy.csv", autodetect_encoding=True)
attempted_encodings: list[str | None] = []
- def fake_open(path, newline="", encoding=None):
+ def fake_open(
+ path: object,
+ newline: str = "",
+ encoding: str | None = None,
+ ) -> _ManagedStringIO:
attempted_encodings.append(encoding)
if encoding is None:
raise UnicodeDecodeError("utf-8", b"x", 0, 1, "decode error")
@@ -99,10 +108,10 @@ class TestCSVExtractor:
assert docs[0].page_content == "id: source-1;body: hello"
assert attempted_encodings == [None, "bad", "utf-8"]
- def test_extract_autodetect_encoding_all_attempts_fail_returns_empty(self, monkeypatch: pytest.MonkeyPatch):
+ def test_extract_autodetect_encoding_all_attempts_fail_returns_empty(self, monkeypatch: pytest.MonkeyPatch) -> None:
extractor = CSVExtractor("dummy.csv", autodetect_encoding=True)
- def always_raise[**P](*args: P.args, **kwargs: P.kwargs):
+ def always_raise[**P](*args: P.args, **kwargs: P.kwargs) -> None:
raise UnicodeDecodeError("utf-8", b"x", 0, 1, "decode error")
monkeypatch.setattr("builtins.open", always_raise)
@@ -110,7 +119,7 @@ class TestCSVExtractor:
assert extractor.extract() == []
- def test_read_from_file_re_raises_csv_error(self, monkeypatch: pytest.MonkeyPatch):
+ def test_read_from_file_re_raises_csv_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
extractor = CSVExtractor("dummy.csv")
monkeypatch.setattr(pd, "read_csv", lambda *args, **kwargs: (_ for _ in ()).throw(csv.Error("bad csv")))
diff --git a/api/tests/unit_tests/core/tools/plugin_tool/test_tool.py b/api/tests/unit_tests/core/tools/plugin_tool/test_tool.py
index dc49d18bf5c..c13de6adb69 100644
--- a/api/tests/unit_tests/core/tools/plugin_tool/test_tool.py
+++ b/api/tests/unit_tests/core/tools/plugin_tool/test_tool.py
@@ -44,7 +44,7 @@ def _build_plugin_tool(*, has_runtime_parameters: bool) -> PluginTool:
@pytest.mark.parametrize("sqlite_session", [()], indirect=True)
-def test_plugin_tool_invoke_and_fork_runtime(sqlite_session: Session):
+def test_plugin_tool_invoke_and_fork_runtime(sqlite_session: Session) -> None:
tool = _build_plugin_tool(has_runtime_parameters=False)
requests: list[httpx.Request] = []
@@ -65,7 +65,7 @@ def test_plugin_tool_invoke_and_fork_runtime(sqlite_session: Session):
):
messages = list(tool.invoke(session=sqlite_session, user_id="user-1", tool_parameters={"raw": 1}))
- assert [m.message.text for m in messages] == ["ok"]
+ assert [m.message.text for m in messages] == ["ok"] # pyrefly: ignore[missing-attribute]
assert len(requests) == 1
request_data = json.loads(requests[0].content)
assert request_data["data"]["tool_parameters"] == {"converted": 1}
@@ -76,7 +76,7 @@ def test_plugin_tool_invoke_and_fork_runtime(sqlite_session: Session):
assert forked.plugin_unique_identifier == tool.plugin_unique_identifier
-def test_plugin_tool_get_runtime_parameters_branches():
+def test_plugin_tool_get_runtime_parameters_branches() -> None:
tool = _build_plugin_tool(has_runtime_parameters=False)
assert tool.get_runtime_parameters() == tool.entity.parameters
diff --git a/api/tests/unit_tests/fields/test_file_fields.py b/api/tests/unit_tests/fields/test_file_fields.py
index 000cd6880b7..5796440a926 100644
--- a/api/tests/unit_tests/fields/test_file_fields.py
+++ b/api/tests/unit_tests/fields/test_file_fields.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+from collections.abc import Callable
from datetime import datetime
import pytest
@@ -8,6 +9,7 @@ from core.workflow.file_reference import build_file_reference
from extensions.storage.storage_type import StorageType
from fields import conversation_fields, message_fields
from fields.file_fields import FileResponse, FileWithSignedUrl, RemoteFileInfo, UploadConfig
+from fields.message_fields import JSONValueType
from graphon.file import File, FileTransferMethod, FileType
from models.enums import CreatorUserRole
from models.model import UploadFile
@@ -30,11 +32,11 @@ def test_file_response_serializes_datetime() -> None:
used=False,
)
file_obj.id = "file-1"
- file_obj.preview_url = "https://preview"
- file_obj.original_url = "https://origin"
- file_obj.user_id = "user-1"
- file_obj.conversation_id = "conv-1"
- file_obj.file_key = "key-1"
+ file_obj.preview_url = "https://preview" # pyrefly: ignore[missing-attribute]
+ file_obj.original_url = "https://origin" # pyrefly: ignore[missing-attribute]
+ file_obj.user_id = "user-1" # pyrefly: ignore[missing-attribute]
+ file_obj.conversation_id = "conv-1" # pyrefly: ignore[missing-attribute]
+ file_obj.file_key = "key-1" # pyrefly: ignore[missing-attribute]
serialized = FileResponse.model_validate(file_obj, from_attributes=True).model_dump(mode="json")
@@ -100,7 +102,9 @@ def test_remote_file_info_and_upload_config() -> None:
message_fields.format_files_contained,
],
)
-def test_file_formatters_preserve_legacy_file_keys(monkeypatch: pytest.MonkeyPatch, formatter) -> None:
+def test_file_formatters_preserve_legacy_file_keys(
+ monkeypatch: pytest.MonkeyPatch, formatter: Callable[[JSONValueType], JSONValueType]
+) -> None:
monkeypatch.setattr(File, "generate_url", lambda self, for_external=True: "https://preview.example/file")
reference = build_file_reference(record_id="upload-1", storage_key="files/source.pdf")
diff --git a/api/tests/unit_tests/libs/test_json_in_md_parser.py b/api/tests/unit_tests/libs/test_json_in_md_parser.py
index 40d454364f4..48b6f749a83 100644
--- a/api/tests/unit_tests/libs/test_json_in_md_parser.py
+++ b/api/tests/unit_tests/libs/test_json_in_md_parser.py
@@ -7,7 +7,7 @@ from libs.json_in_md_parser import (
)
-def test_parse_json_markdown_triple_backticks_json():
+def test_parse_json_markdown_triple_backticks_json() -> None:
src = """
```json
{"a": 1, "b": "x"}
@@ -16,7 +16,7 @@ def test_parse_json_markdown_triple_backticks_json():
assert parse_json_markdown(src) == {"a": 1, "b": "x"}
-def test_parse_json_markdown_triple_backticks_generic():
+def test_parse_json_markdown_triple_backticks_generic() -> None:
src = """
```
{"k": [1, 2, 3]}
@@ -25,22 +25,22 @@ def test_parse_json_markdown_triple_backticks_generic():
assert parse_json_markdown(src) == {"k": [1, 2, 3]}
-def test_parse_json_markdown_single_backticks():
+def test_parse_json_markdown_single_backticks() -> None:
src = '`{"x": true}`'
assert parse_json_markdown(src) == {"x": True}
-def test_parse_json_markdown_braces_only():
+def test_parse_json_markdown_braces_only() -> None:
src = ' {\n \t"ok": "yes"\n} '
assert parse_json_markdown(src) == {"ok": "yes"}
-def test_parse_json_markdown_not_found():
+def test_parse_json_markdown_not_found() -> None:
with pytest.raises(ValueError, match="could not find json block in the output\\."):
parse_json_markdown("no json here")
-def test_parse_and_check_json_markdown_missing_key():
+def test_parse_and_check_json_markdown_missing_key() -> None:
src = """
```
{"present": 1}
@@ -51,7 +51,7 @@ def test_parse_and_check_json_markdown_missing_key():
assert "expected key `missing`" in str(exc.value)
-def test_parse_and_check_json_markdown_invalid_json():
+def test_parse_and_check_json_markdown_invalid_json() -> None:
src = """
```json
{invalid json}
@@ -62,7 +62,7 @@ def test_parse_and_check_json_markdown_invalid_json():
assert "got invalid json object" in str(exc.value)
-def test_parse_and_check_json_markdown_success():
+def test_parse_and_check_json_markdown_success() -> None:
src = """
```json
{"present": 1, "other": 2}
@@ -72,7 +72,7 @@ def test_parse_and_check_json_markdown_success():
assert obj == {"present": 1, "other": 2}
-def test_parse_and_check_json_markdown_multiple_blocks_fails():
+def test_parse_and_check_json_markdown_multiple_blocks_fails() -> None:
src = """
```json
{"a": 1}
@@ -88,7 +88,7 @@ def test_parse_and_check_json_markdown_multiple_blocks_fails():
parse_and_check_json_markdown(src, [])
-def test_parse_and_check_json_markdown_handles_think_fenced_and_raw_variants():
+def test_parse_and_check_json_markdown_handles_think_fenced_and_raw_variants() -> None:
expected = {"keywords": ["2"], "category_id": "2", "category_name": "2"}
cases = [
"""
@@ -109,18 +109,18 @@ def test_parse_and_check_json_markdown_handles_think_fenced_and_raw_variants():
assert obj == expected
-def test_parse_json_markdown_backtick_inside_string_value():
+def test_parse_json_markdown_backtick_inside_string_value() -> None:
"""Backticks inside JSON string values must not be mistaken for code fences."""
src = '{"code": "use `print` function", "n": 1}'
assert parse_json_markdown(src) == {"code": "use `print` function", "n": 1}
-def test_parse_json_markdown_backtick_in_surrounding_prose():
+def test_parse_json_markdown_backtick_in_surrounding_prose() -> None:
"""Backticks in prose before the JSON must not break extraction."""
src = 'Here is `the` result: {"a": 1}'
assert parse_json_markdown(src) == {"a": 1}
-def test_parse_json_markdown_fenced_scalar_still_supported():
+def test_parse_json_markdown_fenced_scalar_still_supported() -> None:
"""Fenced content without brackets still parses via the fence fallback."""
assert parse_json_markdown('```json\n"hello"\n```') == "hello"
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index b99f7352a73..1c1fc0feecd 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -175,7 +175,6 @@ project-excludes = [
"core/app/apps/test_base_app_queue_manager.py",
"core/app/apps/test_exc.py",
"core/app/apps/test_message_based_app_generator.py",
- "core/app/apps/test_message_based_app_queue_manager.py",
"core/app/apps/test_message_generator.py",
"core/app/apps/test_streaming_utils.py",
"core/helper/test_trace_id_helper.py",
@@ -214,7 +213,6 @@ project-excludes = [
"core/entities/test_mcp_provider.py",
"core/entities/test_provider_configuration.py",
"core/extension/test_api_based_extension_requestor.py",
- "core/external_data_tool/test_external_data_fetch.py",
"core/external_data_tool/test_factory.py",
"core/helper/code_executor/test_code_executor.py",
"core/helper/test_ssrf_proxy.py",
@@ -265,7 +263,6 @@ project-excludes = [
"core/rag/datasource/vdb/test_vector_factory.py",
"core/rag/embedding/test_cached_embedding.py",
"core/rag/extractor/blob/test_blob.py",
- "core/rag/extractor/test_csv_extractor.py",
"core/rag/extractor/test_excel_extractor.py",
"core/rag/extractor/test_extract_processor.py",
"core/rag/extractor/test_helpers.py",
@@ -301,7 +298,6 @@ project-excludes = [
"core/tools/utils/test_dataset_retriever_tool.py",
"core/tools/mcp_tool/test_tool.py",
"core/tools/mcp_tool/test_provider.py",
- "core/tools/plugin_tool/test_tool.py",
"core/tools/test_tool_engine.py",
"core/tools/entities/test_tool_entities.py",
"core/tools/test_tool_file_manager.py",
@@ -394,7 +390,6 @@ project-excludes = [
"factories/file_factory/test_remote.py",
"factories/file_factory/test_validation.py",
"fields/test_dataset_fields.py",
- "fields/test_file_fields.py",
"fields/test_message_fields.py",
"../helpers/unit/libs/_human_input/support.py",
"../e2e_tests/contracts/libs/_human_input/test_form_service.py",
@@ -409,7 +404,6 @@ project-excludes = [
"libs/test_external_api.py",
"libs/test_flask_utils.py",
"libs/test_helper.py",
- "libs/test_json_in_md_parser.py",
"libs/test_login.py",
"libs/test_orjson.py",
"libs/test_pagination.py",
@@ -430,7 +424,6 @@ project-excludes = [
"services/agent/test_skill_package_service.py",
"services/agent/test_workflow_publish_service.py",
"../helpers/unit/services/controller_api.py",
- "services/data_migration/test_dependency_discovery_service.py",
"services/data_migration/test_entities.py",
"services/data_migration/test_export_service.py",
"services/data_migration/test_import_service.py",
diff --git a/api/tests/unit_tests/services/data_migration/test_dependency_discovery_service.py b/api/tests/unit_tests/services/data_migration/test_dependency_discovery_service.py
index 92cc951c517..e0c195058ee 100644
--- a/api/tests/unit_tests/services/data_migration/test_dependency_discovery_service.py
+++ b/api/tests/unit_tests/services/data_migration/test_dependency_discovery_service.py
@@ -2,7 +2,7 @@ from services.data_migration.dependency_discovery_service import DependencyDisco
from services.data_migration.entities import DependencyKind
-def test_discovers_and_deduplicates_standalone_tool_nodes():
+def test_discovers_and_deduplicates_standalone_tool_nodes() -> None:
graph = {
"graph": {
"nodes": [
@@ -23,7 +23,7 @@ def test_discovers_and_deduplicates_standalone_tool_nodes():
]
-def test_discovers_agent_node_tools():
+def test_discovers_agent_node_tools() -> None:
graph = {
"graph": {
"nodes": [
@@ -48,7 +48,7 @@ def test_discovers_agent_node_tools():
]
-def test_discovers_tool_nodes_from_exported_workflow_dsl_shape():
+def test_discovers_tool_nodes_from_exported_workflow_dsl_shape() -> None:
dsl = {
"workflow": {
"graph": {
@@ -82,7 +82,7 @@ def test_discovers_tool_nodes_from_exported_workflow_dsl_shape():
]
-def test_discovers_agent_tools_from_exported_agent_parameter_shape():
+def test_discovers_agent_tools_from_exported_agent_parameter_shape() -> None:
dsl = {
"workflow": {
"graph": {