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 MCP error and client session tests (#43628)

Eddy ZHANG0f5973e6d62b
diff --git a/api/tests/unit_tests/core/mcp/client/test_session.py b/api/tests/unit_tests/core/mcp/client/test_session.py
index 8b24c8ce75e..65f6a8a6f52 100644
--- a/api/tests/unit_tests/core/mcp/client/test_session.py
+++ b/api/tests/unit_tests/core/mcp/client/test_session.py
@@ -24,14 +24,14 @@ from core.mcp.types import (
)
-def test_client_session_initialize():
+def test_client_session_initialize() -> None:
# Create synchronous queues to replace async streams
client_to_server: queue.Queue[SessionMessage] = queue.Queue()
server_to_client: queue.Queue[SessionMessage] = queue.Queue()
initialized_notification = None
- def mock_server():
+ def mock_server() -> None:
nonlocal initialized_notification
# Receive initialization request
@@ -83,7 +83,7 @@ def test_client_session_initialize():
# Create message handler
def message_handler(
message: RequestResponder[types.ServerRequest, types.ClientResult] | types.ServerNotification | Exception,
- ):
+ ) -> None:
if isinstance(message, Exception):
raise message
@@ -114,7 +114,7 @@ def test_client_session_initialize():
assert isinstance(initialized_notification.root, InitializedNotification)
-def test_client_session_custom_client_info():
+def test_client_session_custom_client_info() -> None:
# Create synchronous queues to replace async streams
client_to_server: queue.Queue[SessionMessage] = queue.Queue()
server_to_client: queue.Queue[SessionMessage] = queue.Queue()
@@ -122,7 +122,7 @@ def test_client_session_custom_client_info():
custom_client_info = Implementation(name="test-client", version="1.2.3")
received_client_info = None
- def mock_server():
+ def mock_server() -> None:
nonlocal received_client_info
session_message = client_to_server.get(timeout=5.0)
@@ -174,14 +174,14 @@ def test_client_session_custom_client_info():
assert received_client_info == custom_client_info
-def test_client_session_default_client_info():
+def test_client_session_default_client_info() -> None:
# Create synchronous queues to replace async streams
client_to_server: queue.Queue[SessionMessage] = queue.Queue()
server_to_client: queue.Queue[SessionMessage] = queue.Queue()
received_client_info = None
- def mock_server():
+ def mock_server() -> None:
nonlocal received_client_info
session_message = client_to_server.get(timeout=5.0)
@@ -232,12 +232,12 @@ def test_client_session_default_client_info():
assert received_client_info == DEFAULT_CLIENT_INFO
-def test_client_session_version_negotiation_success():
+def test_client_session_version_negotiation_success() -> None:
# Create synchronous queues to replace async streams
client_to_server: queue.Queue[SessionMessage] = queue.Queue()
server_to_client: queue.Queue[SessionMessage] = queue.Queue()
- def mock_server():
+ def mock_server() -> None:
session_message = client_to_server.get(timeout=5.0)
jsonrpc_request = session_message.message
assert isinstance(jsonrpc_request.root, JSONRPCRequest)
@@ -287,12 +287,12 @@ def test_client_session_version_negotiation_success():
assert result.protocolVersion == LATEST_PROTOCOL_VERSION
-def test_client_session_version_negotiation_failure():
+def test_client_session_version_negotiation_failure() -> None:
# Create synchronous queues to replace async streams
client_to_server: queue.Queue[SessionMessage] = queue.Queue()
server_to_client: queue.Queue[SessionMessage] = queue.Queue()
- def mock_server():
+ def mock_server() -> None:
session_message = client_to_server.get(timeout=5.0)
jsonrpc_request = session_message.message
assert isinstance(jsonrpc_request.root, JSONRPCRequest)
@@ -339,14 +339,14 @@ def test_client_session_version_negotiation_failure():
server_thread.join(timeout=10.0)
-def test_client_capabilities_default():
+def test_client_capabilities_default() -> None:
# Create synchronous queues to replace async streams
client_to_server: queue.Queue[SessionMessage] = queue.Queue()
server_to_client: queue.Queue[SessionMessage] = queue.Queue()
received_capabilities = None
- def mock_server():
+ def mock_server() -> None:
nonlocal received_capabilities
session_message = client_to_server.get(timeout=5.0)
@@ -397,7 +397,7 @@ def test_client_capabilities_default():
assert received_capabilities is not None
-def test_client_capabilities_with_custom_callbacks():
+def test_client_capabilities_with_custom_callbacks() -> None:
# Create synchronous queues to replace async streams
client_to_server: queue.Queue[SessionMessage] = queue.Queue()
server_to_client: queue.Queue[SessionMessage] = queue.Queue()
@@ -417,7 +417,7 @@ def test_client_capabilities_with_custom_callbacks():
) -> types.ListRootsResult | types.ErrorData:
return types.ListRootsResult(roots=[])
- def mock_server():
+ def mock_server() -> None:
session_message = client_to_server.get(timeout=5.0)
jsonrpc_request = session_message.message
assert isinstance(jsonrpc_request.root, JSONRPCRequest)
diff --git a/api/tests/unit_tests/core/mcp/test_error.py b/api/tests/unit_tests/core/mcp/test_error.py
index 3a95fae673d..f09425dfa30 100644
--- a/api/tests/unit_tests/core/mcp/test_error.py
+++ b/api/tests/unit_tests/core/mcp/test_error.py
@@ -8,24 +8,24 @@ from core.mcp.error import MCPAuthError, MCPConnectionError, MCPError
class TestMCPError:
"""Test MCPError base exception class."""
- def test_mcp_error_creation(self):
+ def test_mcp_error_creation(self) -> None:
"""Test creating MCPError instance."""
error = MCPError("Test error message")
assert str(error) == "Test error message"
assert isinstance(error, Exception)
- def test_mcp_error_inheritance(self):
+ def test_mcp_error_inheritance(self) -> None:
"""Test MCPError inherits from Exception."""
error = MCPError()
assert isinstance(error, Exception)
assert type(error).__name__ == "MCPError"
- def test_mcp_error_with_empty_message(self):
+ def test_mcp_error_with_empty_message(self) -> None:
"""Test MCPError with empty message."""
error = MCPError()
assert str(error) == ""
- def test_mcp_error_raise(self):
+ def test_mcp_error_raise(self) -> None:
"""Test raising MCPError."""
with pytest.raises(MCPError) as exc_info:
raise MCPError("Something went wrong")
@@ -36,28 +36,28 @@ class TestMCPError:
class TestMCPConnectionError:
"""Test MCPConnectionError exception class."""
- def test_mcp_connection_error_creation(self):
+ def test_mcp_connection_error_creation(self) -> None:
"""Test creating MCPConnectionError instance."""
error = MCPConnectionError("Connection failed")
assert str(error) == "Connection failed"
assert isinstance(error, MCPError)
assert isinstance(error, Exception)
- def test_mcp_connection_error_inheritance(self):
+ def test_mcp_connection_error_inheritance(self) -> None:
"""Test MCPConnectionError inheritance chain."""
error = MCPConnectionError()
assert isinstance(error, MCPConnectionError)
assert isinstance(error, MCPError)
assert isinstance(error, Exception)
- def test_mcp_connection_error_raise(self):
+ def test_mcp_connection_error_raise(self) -> None:
"""Test raising MCPConnectionError."""
with pytest.raises(MCPConnectionError) as exc_info:
raise MCPConnectionError("Unable to connect to server")
assert str(exc_info.value) == "Unable to connect to server"
- def test_mcp_connection_error_catch_as_mcp_error(self):
+ def test_mcp_connection_error_catch_as_mcp_error(self) -> None:
"""Test catching MCPConnectionError as MCPError."""
with pytest.raises(MCPError) as exc_info:
raise MCPConnectionError("Connection issue")
@@ -69,7 +69,7 @@ class TestMCPConnectionError:
class TestMCPAuthError:
"""Test MCPAuthError exception class."""
- def test_mcp_auth_error_creation(self):
+ def test_mcp_auth_error_creation(self) -> None:
"""Test creating MCPAuthError instance."""
error = MCPAuthError("Authentication failed")
assert str(error) == "Authentication failed"
@@ -77,7 +77,7 @@ class TestMCPAuthError:
assert isinstance(error, MCPError)
assert isinstance(error, Exception)
- def test_mcp_auth_error_inheritance(self):
+ def test_mcp_auth_error_inheritance(self) -> None:
"""Test MCPAuthError inheritance chain."""
error = MCPAuthError()
assert isinstance(error, MCPAuthError)
@@ -85,14 +85,14 @@ class TestMCPAuthError:
assert isinstance(error, MCPError)
assert isinstance(error, Exception)
- def test_mcp_auth_error_raise(self):
+ def test_mcp_auth_error_raise(self) -> None:
"""Test raising MCPAuthError."""
with pytest.raises(MCPAuthError) as exc_info:
raise MCPAuthError("Invalid credentials")
assert str(exc_info.value) == "Invalid credentials"
- def test_mcp_auth_error_catch_hierarchy(self):
+ def test_mcp_auth_error_catch_hierarchy(self) -> None:
"""Test catching MCPAuthError at different levels."""
# Catch as MCPAuthError
with pytest.raises(MCPAuthError) as exc_info:
@@ -115,7 +115,7 @@ class TestMCPAuthError:
class TestErrorHierarchy:
"""Test the complete error hierarchy."""
- def test_exception_hierarchy(self):
+ def test_exception_hierarchy(self) -> None:
"""Test the complete exception hierarchy."""
# Create instances
base_error = MCPError("base")
@@ -132,16 +132,16 @@ class TestErrorHierarchy:
assert isinstance(auth_error, MCPError)
assert isinstance(auth_error, MCPConnectionError)
- def test_error_handling_patterns(self):
+ def test_error_handling_patterns(self) -> None:
"""Test common error handling patterns."""
- def raise_auth_error():
+ def raise_auth_error() -> None:
raise MCPAuthError("401 Unauthorized")
- def raise_connection_error():
+ def raise_connection_error() -> None:
raise MCPConnectionError("Connection timeout")
- def raise_base_error():
+ def raise_base_error() -> None:
raise MCPError("Generic error")
# Pattern 1: Catch specific errors first
@@ -165,11 +165,11 @@ class TestErrorHierarchy:
error_func()
assert isinstance(exc_info.value, MCPError)
- def test_error_with_cause(self):
+ def test_error_with_cause(self) -> None:
"""Test errors with cause (chained exceptions)."""
original_error = ValueError("Original error")
- def raise_chained_error():
+ def raise_chained_error() -> None:
try:
raise original_error
except ValueError as e:
@@ -181,7 +181,7 @@ class TestErrorHierarchy:
assert str(exc_info.value) == "Connection failed"
assert exc_info.value.__cause__ == original_error
- def test_error_comparison(self):
+ def test_error_comparison(self) -> None:
"""Test error instance comparison."""
error1 = MCPError("Test message")
error2 = MCPError("Test message")
@@ -194,7 +194,7 @@ class TestErrorHierarchy:
# But they have the same type
assert type(error1) == type(error2) == type(error3)
- def test_error_representation(self):
+ def test_error_representation(self) -> None:
"""Test error string representation."""
base_error = MCPError("Base error message")
connection_error = MCPConnectionError("Connection error message")
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index 90d2cea5375..6c07a7561ea 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -377,7 +377,6 @@ project-excludes = [
"core/logging/test_filters.py",
"core/logging/test_trace_helpers.py",
"core/mcp/auth/test_auth_flow.py",
- "core/mcp/client/test_session.py",
"core/mcp/client/test_sse.py",
"core/mcp/client/test_streamable_http.py",
"core/mcp/server/test_streamable_http.py",
@@ -385,7 +384,6 @@ project-excludes = [
"core/mcp/session/test_client_session.py",
"core/mcp/test_auth_client_inheritance.py",
"core/mcp/test_entities.py",
- "core/mcp/test_error.py",
"core/mcp/test_mcp_client.py",
"core/mcp/test_types.py",
"core/mcp/test_utils.py",