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 workflow model tests (#43874)

Eddy ZHANGff066a215baf
diff --git a/api/tests/unit_tests/models/test_workflow_models.py b/api/tests/unit_tests/models/test_workflow_models.py
index 0953570a315..da2804bc9ba 100644
--- a/api/tests/unit_tests/models/test_workflow_models.py
+++ b/api/tests/unit_tests/models/test_workflow_models.py
@@ -32,7 +32,7 @@ from models.workflow import (
class TestWorkflowModelValidation:
"""Test suite for Workflow model validation and basic operations."""
- def test_workflow_creation_with_required_fields(self):
+ def test_workflow_creation_with_required_fields(self) -> None:
"""Test creating a workflow with all required fields."""
# Arrange
tenant_id = str(uuid4())
@@ -65,14 +65,14 @@ class TestWorkflowModelValidation:
assert workflow.created_at is not None
assert workflow.updated_at is not None
- def test_workflow_type_enum_values(self):
+ def test_workflow_type_enum_values(self) -> None:
"""Test WorkflowType enum values."""
# Assert
assert WorkflowType.WORKFLOW == "workflow"
assert WorkflowType.CHAT.value == "chat"
assert WorkflowType.RAG_PIPELINE.value == "rag-pipeline"
- def test_workflow_type_value_of(self):
+ def test_workflow_type_value_of(self) -> None:
"""Test WorkflowType.value_of method."""
# Act & Assert
assert WorkflowType.value_of("workflow") == WorkflowType.WORKFLOW
@@ -82,10 +82,10 @@ class TestWorkflowModelValidation:
with pytest.raises(ValueError, match="invalid workflow type value"):
WorkflowType.value_of("invalid_type")
- def test_workflow_graph_dict_property(self):
+ def test_workflow_graph_dict_property(self) -> None:
"""Test graph_dict property parses JSON correctly."""
# Arrange
- graph_data = {"nodes": [{"id": "start", "type": "start"}], "edges": []}
+ graph_data = {"nodes": [{"id": "start", "type": "start"}], "edges": list[object]()}
workflow = Workflow.new(
tenant_id=str(uuid4()),
app_id=str(uuid4()),
@@ -107,7 +107,7 @@ class TestWorkflowModelValidation:
assert "nodes" in result
assert len(result["nodes"]) == 1
- def test_workflow_features_dict_property(self):
+ def test_workflow_features_dict_property(self) -> None:
"""Test features_dict property parses JSON correctly."""
# Arrange
features_data = {"file_upload": {"enabled": True, "max_files": 5}}
@@ -132,7 +132,7 @@ class TestWorkflowModelValidation:
assert result["file_upload"]["enabled"] is True
assert result["file_upload"]["max_files"] == 5
- def test_workflow_with_marked_name_and_comment(self):
+ def test_workflow_with_marked_name_and_comment(self) -> None:
"""Test workflow creation with marked name and comment."""
# Arrange & Act
workflow = Workflow.new(
@@ -154,7 +154,7 @@ class TestWorkflowModelValidation:
assert workflow.marked_name == "Production Release"
assert workflow.marked_comment == "Initial production version"
- def test_workflow_version_draft_constant(self):
+ def test_workflow_version_draft_constant(self) -> None:
"""Test VERSION_DRAFT constant."""
# Assert
assert Workflow.VERSION_DRAFT == "draft"
@@ -163,7 +163,7 @@ class TestWorkflowModelValidation:
class TestWorkflowRunStateTransitions:
"""Test suite for WorkflowRun state transitions and lifecycle."""
- def test_workflow_run_creation_with_required_fields(self):
+ def test_workflow_run_creation_with_required_fields(self) -> None:
"""Test creating a workflow run with required fields."""
# Arrange
tenant_id = str(uuid4())
@@ -193,7 +193,7 @@ class TestWorkflowRunStateTransitions:
assert workflow_run.status == WorkflowExecutionStatus.RUNNING
assert workflow_run.created_by == created_by
- def test_workflow_run_state_transition_running_to_succeeded(self):
+ def test_workflow_run_state_transition_running_to_succeeded(self) -> None:
"""Test state transition from running to succeeded."""
# Arrange
workflow_run = WorkflowRun(
@@ -218,7 +218,7 @@ class TestWorkflowRunStateTransitions:
assert workflow_run.finished_at is not None
assert workflow_run.elapsed_time == 2.5
- def test_workflow_run_state_transition_running_to_failed(self):
+ def test_workflow_run_state_transition_running_to_failed(self) -> None:
"""Test state transition from running to failed with error."""
# Arrange
workflow_run = WorkflowRun(
@@ -243,7 +243,7 @@ class TestWorkflowRunStateTransitions:
assert workflow_run.error == "Node execution failed: Invalid input"
assert workflow_run.finished_at is not None
- def test_workflow_run_state_transition_running_to_stopped(self):
+ def test_workflow_run_state_transition_running_to_stopped(self) -> None:
"""Test state transition from running to stopped."""
# Arrange
workflow_run = WorkflowRun(
@@ -266,7 +266,7 @@ class TestWorkflowRunStateTransitions:
assert workflow_run.status == WorkflowExecutionStatus.STOPPED
assert workflow_run.finished_at is not None
- def test_workflow_run_state_transition_running_to_paused(self):
+ def test_workflow_run_state_transition_running_to_paused(self) -> None:
"""Test state transition from running to paused."""
# Arrange
workflow_run = WorkflowRun(
@@ -288,7 +288,7 @@ class TestWorkflowRunStateTransitions:
assert workflow_run.status == WorkflowExecutionStatus.PAUSED
assert workflow_run.finished_at is None # Not finished when paused
- def test_workflow_run_state_transition_paused_to_running(self):
+ def test_workflow_run_state_transition_paused_to_running(self) -> None:
"""Test state transition from paused back to running."""
# Arrange
workflow_run = WorkflowRun(
@@ -309,7 +309,7 @@ class TestWorkflowRunStateTransitions:
# Assert
assert workflow_run.status == WorkflowExecutionStatus.RUNNING
- def test_workflow_run_with_partial_succeeded_status(self):
+ def test_workflow_run_with_partial_succeeded_status(self) -> None:
"""Test workflow run with partial-succeeded status."""
# Arrange & Act
workflow_run = WorkflowRun(
@@ -329,7 +329,7 @@ class TestWorkflowRunStateTransitions:
assert workflow_run.status == WorkflowExecutionStatus.PARTIAL_SUCCEEDED
assert workflow_run.exceptions_count == 2
- def test_workflow_run_with_inputs_and_outputs(self):
+ def test_workflow_run_with_inputs_and_outputs(self) -> None:
"""Test workflow run with inputs and outputs as JSON."""
# Arrange
inputs = {"query": "What is AI?", "context": "technology"}
@@ -354,10 +354,10 @@ class TestWorkflowRunStateTransitions:
assert workflow_run.inputs_dict == inputs
assert workflow_run.outputs_dict == outputs
- def test_workflow_run_graph_dict_property(self):
+ def test_workflow_run_graph_dict_property(self) -> None:
"""Test graph_dict property for workflow run."""
# Arrange
- graph = {"nodes": [{"id": "start", "type": "start"}], "edges": []}
+ graph = {"nodes": [{"id": "start", "type": "start"}], "edges": list[object]()}
workflow_run = WorkflowRun(
tenant_id=str(uuid4()),
app_id=str(uuid4()),
@@ -378,7 +378,7 @@ class TestWorkflowRunStateTransitions:
assert result == graph
assert "nodes" in result
- def test_workflow_run_to_dict_serialization(self):
+ def test_workflow_run_to_dict_serialization(self) -> None:
"""Test WorkflowRun to_dict method."""
# Arrange
workflow_run_id = str(uuid4())
@@ -414,7 +414,7 @@ class TestWorkflowRunStateTransitions:
assert result["total_tokens"] == 1500
assert result["total_steps"] == 5
- def test_workflow_run_from_dict_deserialization(self):
+ def test_workflow_run_from_dict_deserialization(self) -> None:
"""Test WorkflowRun from_dict method."""
# Arrange
data = {
@@ -425,7 +425,7 @@ class TestWorkflowRunStateTransitions:
"type": WorkflowType.WORKFLOW,
"triggered_from": WorkflowRunTriggeredFrom.APP_RUN,
"version": "v1.0",
- "graph": {"nodes": [], "edges": []},
+ "graph": {"nodes": list[object](), "edges": list[object]()},
"inputs": {"query": "test"},
"status": WorkflowExecutionStatus.SUCCEEDED,
"outputs": {"result": "success"},
@@ -453,7 +453,7 @@ class TestWorkflowRunStateTransitions:
class TestNodeExecutionRelationships:
"""Test suite for WorkflowNodeExecutionModel relationships and data."""
- def test_node_execution_creation_with_required_fields(self):
+ def test_node_execution_creation_with_required_fields(self) -> None:
"""Test creating a node execution with required fields."""
# Arrange
tenant_id = str(uuid4())
@@ -487,7 +487,7 @@ class TestNodeExecutionRelationships:
assert node_execution.node_type == BuiltinNodeTypes.START
assert node_execution.index == 1
- def test_node_execution_with_predecessor_relationship(self):
+ def test_node_execution_with_predecessor_relationship(self) -> None:
"""Test node execution with predecessor node relationship."""
# Arrange
predecessor_node_id = "start"
@@ -515,7 +515,7 @@ class TestNodeExecutionRelationships:
assert node_execution.node_id == current_node_id
assert node_execution.index == 2
- def test_node_execution_single_step_debugging(self):
+ def test_node_execution_single_step_debugging(self) -> None:
"""Test node execution for single-step debugging (no workflow_run_id)."""
# Arrange & Act
node_execution = WorkflowNodeExecutionModel(
@@ -537,7 +537,7 @@ class TestNodeExecutionRelationships:
assert node_execution.triggered_from == WorkflowNodeExecutionTriggeredFrom.SINGLE_STEP.value
assert node_execution.workflow_run_id is None
- def test_node_execution_with_inputs_outputs_process_data(self):
+ def test_node_execution_with_inputs_outputs_process_data(self) -> None:
"""Test node execution with inputs, outputs, and process_data."""
# Arrange
inputs = {"query": "What is AI?", "temperature": 0.7}
@@ -568,7 +568,7 @@ class TestNodeExecutionRelationships:
assert node_execution.outputs_dict == outputs
assert node_execution.process_data_dict == process_data
- def test_node_execution_status_transitions(self):
+ def test_node_execution_status_transitions(self) -> None:
"""Test node execution status transitions."""
# Arrange
node_execution = WorkflowNodeExecutionModel(
@@ -596,7 +596,7 @@ class TestNodeExecutionRelationships:
assert node_execution.elapsed_time == 1.2
assert node_execution.finished_at is not None
- def test_node_execution_with_error(self):
+ def test_node_execution_with_error(self) -> None:
"""Test node execution with error status."""
# Arrange
error_message = "Code execution failed: SyntaxError on line 5"
@@ -622,7 +622,7 @@ class TestNodeExecutionRelationships:
assert node_execution.status == WorkflowNodeExecutionStatus.FAILED
assert node_execution.error == error_message
- def test_node_execution_with_metadata(self):
+ def test_node_execution_with_metadata(self) -> None:
"""Test node execution with execution metadata."""
# Arrange
metadata = {
@@ -653,7 +653,7 @@ class TestNodeExecutionRelationships:
assert node_execution.execution_metadata_dict == metadata
assert node_execution.execution_metadata_dict["total_tokens"] == 500
- def test_node_execution_metadata_dict_empty(self):
+ def test_node_execution_metadata_dict_empty(self) -> None:
"""Test execution_metadata_dict returns empty dict when metadata is None."""
# Arrange
node_execution = WorkflowNodeExecutionModel(
@@ -678,7 +678,7 @@ class TestNodeExecutionRelationships:
# Assert
assert result == {}
- def test_node_execution_different_node_types(self):
+ def test_node_execution_different_node_types(self) -> None:
"""Test node execution with different node types."""
# Test various node types
node_types = [
@@ -715,7 +715,7 @@ class TestNodeExecutionRelationships:
class TestGraphConfigurationValidation:
"""Test suite for graph configuration validation."""
- def test_workflow_graph_with_nodes_and_edges(self):
+ def test_workflow_graph_with_nodes_and_edges(self) -> None:
"""Test workflow graph configuration with nodes and edges."""
# Arrange
graph_config = {
@@ -752,10 +752,10 @@ class TestGraphConfigurationValidation:
assert graph_dict["edges"][0]["source"] == "start"
assert graph_dict["edges"][0]["target"] == "llm_1"
- def test_workflow_graph_empty_configuration(self):
+ def test_workflow_graph_empty_configuration(self) -> None:
"""Test workflow with empty graph configuration."""
# Arrange
- graph_config = {"nodes": [], "edges": []}
+ graph_config = {"nodes": list[object](), "edges": list[object]()}
# Act
workflow = Workflow.new(
@@ -776,7 +776,7 @@ class TestGraphConfigurationValidation:
assert graph_dict["nodes"] == []
assert graph_dict["edges"] == []
- def test_workflow_graph_complex_node_data(self):
+ def test_workflow_graph_complex_node_data(self) -> None:
"""Test workflow graph with complex node data structures."""
# Arrange
graph_config = {
@@ -795,7 +795,7 @@ class TestGraphConfigurationValidation:
},
}
],
- "edges": [],
+ "edges": list[object](),
}
# Act
@@ -819,7 +819,7 @@ class TestGraphConfigurationValidation:
assert node_data["model_parameters"]["temperature"] == 0.7
assert len(node_data["prompt_template"]) == 2
- def test_workflow_run_graph_preservation(self):
+ def test_workflow_run_graph_preservation(self) -> None:
"""Test that WorkflowRun preserves graph configuration from Workflow."""
# Arrange
original_graph = {
@@ -848,13 +848,13 @@ class TestGraphConfigurationValidation:
assert workflow_run.graph_dict == original_graph
assert len(workflow_run.graph_dict["nodes"]) == 2
- def test_workflow_graph_with_conditional_branches(self):
+ def test_workflow_graph_with_conditional_branches(self) -> None:
"""Test workflow graph with conditional branching (if-else)."""
# Arrange
graph_config = {
"nodes": [
{"id": "start", "type": "start"},
- {"id": "if_else_1", "type": "if-else", "data": {"conditions": []}},
+ {"id": "if_else_1", "type": "if-else", "data": {"conditions": list[object]()}},
{"id": "branch_true", "type": "llm"},
{"id": "branch_false", "type": "code"},
{"id": "end", "type": "end"},
@@ -890,7 +890,7 @@ class TestGraphConfigurationValidation:
conditional_edges = [e for e in graph_dict["edges"] if "sourceHandle" in e]
assert len(conditional_edges) == 2
- def test_workflow_graph_with_loop_structure(self):
+ def test_workflow_graph_with_loop_structure(self) -> None:
"""Test workflow graph with loop/iteration structure."""
# Arrange
graph_config = {
@@ -927,7 +927,7 @@ class TestGraphConfigurationValidation:
iteration_node = next(n for n in graph_dict["nodes"] if n["type"] == "iteration")
assert iteration_node["data"]["iterator"] == "items"
- def test_workflow_graph_dict_with_null_graph(self):
+ def test_workflow_graph_dict_with_null_graph(self) -> None:
"""Test graph_dict property when graph is None."""
# Arrange
workflow = Workflow.new(
@@ -935,7 +935,7 @@ class TestGraphConfigurationValidation:
app_id=str(uuid4()),
type=WorkflowType.WORKFLOW,
version="draft",
- graph=None,
+ graph=None, # pyrefly: ignore[bad-argument-type]
features="{}",
created_by=str(uuid4()),
environment_variables=[],
@@ -949,7 +949,7 @@ class TestGraphConfigurationValidation:
# Assert
assert result == {}
- def test_workflow_run_inputs_dict_with_null_inputs(self):
+ def test_workflow_run_inputs_dict_with_null_inputs(self) -> None:
"""Test inputs_dict property when inputs is None."""
# Arrange
workflow_run = WorkflowRun(
@@ -971,7 +971,7 @@ class TestGraphConfigurationValidation:
# Assert
assert result == {}
- def test_workflow_run_outputs_dict_with_null_outputs(self):
+ def test_workflow_run_outputs_dict_with_null_outputs(self) -> None:
"""Test outputs_dict property when outputs is None."""
# Arrange
workflow_run = WorkflowRun(
@@ -993,7 +993,7 @@ class TestGraphConfigurationValidation:
# Assert
assert result == {}
- def test_node_execution_inputs_dict_with_null_inputs(self):
+ def test_node_execution_inputs_dict_with_null_inputs(self) -> None:
"""Test node execution inputs_dict when inputs is None."""
# Arrange
node_execution = WorkflowNodeExecutionModel(
@@ -1018,7 +1018,7 @@ class TestGraphConfigurationValidation:
# Assert
assert result is None
- def test_node_execution_outputs_dict_with_null_outputs(self):
+ def test_node_execution_outputs_dict_with_null_outputs(self) -> None:
"""Test node execution outputs_dict when outputs is None."""
# Arrange
node_execution = WorkflowNodeExecutionModel(
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index b6d3d9a4dd8..9ca8fcb23da 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -675,7 +675,6 @@ project-excludes = [
"models/test_tool_models.py",
"models/test_types.py",
"models/test_workflow.py",
- "models/test_workflow_models.py",
"models/test_workflow_node_execution_offload.py",
"oss/__mock/aliyun_oss.py",
"oss/__mock/baidu_obs.py",