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 entity and type tests (#43880)

Eddy ZHANG61dd17976831
diff --git a/api/tests/unit_tests/core/mcp/test_auth_client_inheritance.py b/api/tests/unit_tests/core/mcp/test_auth_client_inheritance.py
index 643aa5f772f..b6ffd63d239 100644
--- a/api/tests/unit_tests/core/mcp/test_auth_client_inheritance.py
+++ b/api/tests/unit_tests/core/mcp/test_auth_client_inheritance.py
@@ -7,7 +7,7 @@ from core.mcp.error import MCPAuthError
class TestForwardIdentityShortCircuit:
- def test_forward_identity_active_reraises_without_retry(self):
+ def test_forward_identity_active_reraises_without_retry(self) -> None:
client = MCPClientWithAuthRetry(
server_url="https://mcp.example.com",
headers={"Authorization": "Bearer user-jwt"},
@@ -20,7 +20,7 @@ class TestForwardIdentityShortCircuit:
assert client.headers["Authorization"] == "Bearer user-jwt"
assert client._has_retried is False
- def test_forward_identity_active_takes_precedence_over_provider_entity(self):
+ def test_forward_identity_active_takes_precedence_over_provider_entity(self) -> None:
sentinel_entity = object()
client = MCPClientWithAuthRetry(
server_url="https://mcp.example.com",
@@ -31,11 +31,11 @@ class TestForwardIdentityShortCircuit:
with pytest.raises(MCPAuthError, match="forwarded-id-401"):
client._handle_auth_error(MCPAuthError("forwarded-id-401"))
- def test_default_path_unchanged_without_provider_entity(self):
+ def test_default_path_unchanged_without_provider_entity(self) -> None:
client = MCPClientWithAuthRetry(server_url="https://mcp.example.com")
with pytest.raises(MCPAuthError, match="no-provider"):
client._handle_auth_error(MCPAuthError("no-provider"))
- def test_default_constructor_defaults_forward_identity_to_false(self):
+ def test_default_constructor_defaults_forward_identity_to_false(self) -> None:
client = MCPClientWithAuthRetry(server_url="https://mcp.example.com")
assert client.forward_identity_active is False
diff --git a/api/tests/unit_tests/core/mcp/test_entities.py b/api/tests/unit_tests/core/mcp/test_entities.py
index d591a42ae20..e7354fff551 100644
--- a/api/tests/unit_tests/core/mcp/test_entities.py
+++ b/api/tests/unit_tests/core/mcp/test_entities.py
@@ -7,10 +7,22 @@ from core.mcp.entities import (
RequestContext,
)
from core.mcp.session.base_session import BaseSession
-from core.mcp.types import LATEST_PROTOCOL_VERSION, RequestParams, ServerNotification, ServerRequest
+from core.mcp.types import (
+ LATEST_PROTOCOL_VERSION,
+ ClientNotification,
+ ClientRequest,
+ ClientResult,
+ RequestParams,
+ ServerNotification,
+ ServerRequest,
+)
+
+type _ClientSideSession = BaseSession[
+ ClientRequest, ClientNotification, ClientResult, ServerRequest, ServerNotification
+]
-def _session() -> BaseSession:
+def _session() -> _ClientSideSession:
return BaseSession(
read_stream=Queue(),
write_stream=Queue(),
@@ -22,7 +34,7 @@ def _session() -> BaseSession:
class TestProtocolVersions:
"""Test protocol version constants."""
- def test_supported_protocol_versions(self):
+ def test_supported_protocol_versions(self) -> None:
"""Test supported protocol versions list."""
assert isinstance(SUPPORTED_PROTOCOL_VERSIONS, list)
assert len(SUPPORTED_PROTOCOL_VERSIONS) >= 3
@@ -30,7 +42,7 @@ class TestProtocolVersions:
assert "2025-03-26" in SUPPORTED_PROTOCOL_VERSIONS
assert LATEST_PROTOCOL_VERSION in SUPPORTED_PROTOCOL_VERSIONS
- def test_latest_protocol_version_is_supported(self):
+ def test_latest_protocol_version_is_supported(self) -> None:
"""Test that latest protocol version is in supported versions."""
assert LATEST_PROTOCOL_VERSION in SUPPORTED_PROTOCOL_VERSIONS
@@ -38,7 +50,7 @@ class TestProtocolVersions:
class TestRequestContext:
"""Test RequestContext dataclass."""
- def test_request_context_creation(self):
+ def test_request_context_creation(self) -> None:
"""Test creating a RequestContext instance."""
session = _session()
lifespan = {"key": "value"}
@@ -56,7 +68,7 @@ class TestRequestContext:
assert context.session == session
assert context.lifespan_context == lifespan
- def test_request_context_with_none_meta(self):
+ def test_request_context_with_none_meta(self) -> None:
"""Test creating RequestContext with None meta."""
session = _session()
@@ -72,7 +84,7 @@ class TestRequestContext:
assert context.session == session
assert context.lifespan_context is None
- def test_request_context_attributes(self):
+ def test_request_context_attributes(self) -> None:
"""Test RequestContext attributes are accessible."""
session = _session()
@@ -95,13 +107,13 @@ class TestRequestContext:
assert context.session == session
assert context.lifespan_context is None
- def test_request_context_generic_typing(self):
+ def test_request_context_generic_typing(self) -> None:
"""Test RequestContext with different generic types."""
# Create a real session without starting its receive loop.
session = _session()
# Create context with string lifespan context
- context_str = RequestContext[BaseSession, str](
+ context_str = RequestContext[_ClientSideSession, str](
request_id="test-1",
meta=None,
session=session,
@@ -110,7 +122,7 @@ class TestRequestContext:
assert isinstance(context_str.lifespan_context, str)
# Create context with dict lifespan context
- context_dict = RequestContext[BaseSession, dict](
+ context_dict = RequestContext[_ClientSideSession, dict[str, str]](
request_id="test-2",
meta=None,
session=session,
@@ -120,11 +132,11 @@ class TestRequestContext:
# Create context with custom object lifespan context
class CustomLifespan:
- def __init__(self, data):
+ def __init__(self, data: str) -> None:
self.data = data
custom_lifespan = CustomLifespan("test-data")
- context_custom = RequestContext[BaseSession, CustomLifespan](
+ context_custom = RequestContext[_ClientSideSession, CustomLifespan](
request_id="test-3",
meta=None,
session=session,
@@ -133,7 +145,7 @@ class TestRequestContext:
assert isinstance(context_custom.lifespan_context, CustomLifespan)
assert context_custom.lifespan_context.data == "test-data"
- def test_request_context_with_progress_meta(self):
+ def test_request_context_with_progress_meta(self) -> None:
"""Test RequestContext with progress metadata."""
session = _session()
progress_meta = RequestParams.Meta(progressToken="progress-123")
@@ -148,7 +160,7 @@ class TestRequestContext:
assert context.meta is not None
assert context.meta.progressToken == "progress-123"
- def test_request_context_equality(self):
+ def test_request_context_equality(self) -> None:
"""Test RequestContext equality comparison."""
session1 = _session()
session2 = _session()
@@ -189,7 +201,7 @@ class TestRequestContext:
)
assert context1 != context4
- def test_request_context_repr(self):
+ def test_request_context_repr(self) -> None:
"""Test RequestContext string representation."""
session = _session()
diff --git a/api/tests/unit_tests/core/mcp/test_types.py b/api/tests/unit_tests/core/mcp/test_types.py
index ff16f1f9dc8..996fa1ccbbd 100644
--- a/api/tests/unit_tests/core/mcp/test_types.py
+++ b/api/tests/unit_tests/core/mcp/test_types.py
@@ -58,14 +58,14 @@ from core.mcp.types import (
class TestConstants:
"""Test module constants."""
- def test_protocol_versions(self):
+ def test_protocol_versions(self) -> None:
"""Test protocol version constants."""
assert LATEST_PROTOCOL_VERSION == "2025-06-18"
assert SERVER_LATEST_PROTOCOL_VERSION == "2025-06-18"
assert DEFAULT_NEGOTIATED_VERSION == "2025-03-26"
assert sorted(SERVER_SUPPORTED_PROTOCOL_VERSIONS) == ["2024-11-05", "2025-03-26", "2025-06-18"]
- def test_error_codes(self):
+ def test_error_codes(self) -> None:
"""Test JSON-RPC error code constants."""
assert PARSE_ERROR == -32700
assert INVALID_REQUEST == -32600
@@ -77,25 +77,25 @@ class TestConstants:
class TestRequestParams:
"""Test RequestParams and related classes."""
- def test_request_params_basic(self):
+ def test_request_params_basic(self) -> None:
"""Test basic RequestParams creation."""
params = RequestParams()
assert params.meta is None
- def test_request_params_with_meta(self):
+ def test_request_params_with_meta(self) -> None:
"""Test RequestParams with meta."""
meta = RequestParams.Meta(progressToken="test-token")
params = RequestParams(_meta=meta)
assert params.meta is not None
assert params.meta.progressToken == "test-token"
- def test_request_params_meta_extra_fields(self):
+ def test_request_params_meta_extra_fields(self) -> None:
"""Test RequestParams.Meta allows extra fields."""
meta = RequestParams.Meta(progressToken="token", customField="value")
assert meta.progressToken == "token"
assert meta.customField == "value" # type: ignore
- def test_request_params_serialization(self):
+ def test_request_params_serialization(self) -> None:
"""Test RequestParams serialization with _meta alias."""
meta = RequestParams.Meta(progressToken="test")
params = RequestParams(_meta=meta)
@@ -110,7 +110,7 @@ class TestRequestParams:
class TestJSONRPCMessages:
"""Test JSON-RPC message types."""
- def test_jsonrpc_request(self):
+ def test_jsonrpc_request(self) -> None:
"""Test JSONRPCRequest creation and validation."""
request = JSONRPCRequest(jsonrpc="2.0", id="test-123", method="test_method", params={"key": "value"})
@@ -119,12 +119,12 @@ class TestJSONRPCMessages:
assert request.method == "test_method"
assert request.params == {"key": "value"}
- def test_jsonrpc_request_numeric_id(self):
+ def test_jsonrpc_request_numeric_id(self) -> None:
"""Test JSONRPCRequest with numeric ID."""
request = JSONRPCRequest(jsonrpc="2.0", id=123, method="test", params=None)
assert request.id == 123
- def test_jsonrpc_notification(self):
+ def test_jsonrpc_notification(self) -> None:
"""Test JSONRPCNotification creation."""
notification = JSONRPCNotification(jsonrpc="2.0", method="notification_method", params={"data": "test"})
@@ -132,7 +132,7 @@ class TestJSONRPCMessages:
assert notification.method == "notification_method"
assert not hasattr(notification, "id") # Notifications don't have ID
- def test_jsonrpc_response(self):
+ def test_jsonrpc_response(self) -> None:
"""Test JSONRPCResponse creation."""
response = JSONRPCResponse(jsonrpc="2.0", id="req-123", result={"success": True})
@@ -140,7 +140,7 @@ class TestJSONRPCMessages:
assert response.id == "req-123"
assert response.result == {"success": True}
- def test_jsonrpc_error(self):
+ def test_jsonrpc_error(self) -> None:
"""Test JSONRPCError creation."""
error_data = ErrorData(code=INVALID_PARAMS, message="Invalid parameters", data={"field": "missing"})
@@ -152,7 +152,7 @@ class TestJSONRPCMessages:
assert error.error.message == "Invalid parameters"
assert error.error.data == {"field": "missing"}
- def test_jsonrpc_message_parsing(self):
+ def test_jsonrpc_message_parsing(self) -> None:
"""Test JSONRPCMessage parsing different message types."""
# Parse request
request_json = '{"jsonrpc": "2.0", "id": 1, "method": "test", "params": null}'
@@ -173,7 +173,7 @@ class TestJSONRPCMessages:
class TestCapabilities:
"""Test capability classes."""
- def test_client_capabilities(self):
+ def test_client_capabilities(self) -> None:
"""Test ClientCapabilities creation."""
caps = ClientCapabilities(
experimental={"feature": {"enabled": True}},
@@ -185,7 +185,7 @@ class TestCapabilities:
assert caps.sampling is not None
assert caps.roots.listChanged is True # type: ignore
- def test_server_capabilities(self):
+ def test_server_capabilities(self) -> None:
"""Test ServerCapabilities creation."""
caps = ServerCapabilities(
tools={"listChanged": True},
@@ -203,7 +203,7 @@ class TestCapabilities:
class TestInitialization:
"""Test initialization request/response types."""
- def test_initialize_request(self):
+ def test_initialize_request(self) -> None:
"""Test InitializeRequest creation."""
client_info = Implementation(name="test-client", version="1.0.0")
capabilities = ClientCapabilities()
@@ -218,7 +218,7 @@ class TestInitialization:
assert request.params.protocolVersion == LATEST_PROTOCOL_VERSION
assert request.params.clientInfo.name == "test-client"
- def test_initialize_result(self):
+ def test_initialize_result(self) -> None:
"""Test InitializeResult creation."""
server_info = Implementation(name="test-server", version="1.0.0")
capabilities = ServerCapabilities()
@@ -238,7 +238,7 @@ class TestInitialization:
class TestTools:
"""Test tool-related types."""
- def test_tool_creation(self):
+ def test_tool_creation(self) -> None:
"""Test Tool creation with all fields."""
tool = Tool(
name="test_tool",
@@ -255,9 +255,10 @@ class TestTools:
assert tool.title == "Test Tool"
assert tool.description == "A tool for testing"
assert tool.inputSchema["properties"]["input"]["type"] == "string"
+ assert tool.annotations is not None
assert tool.annotations.idempotentHint is True
- def test_call_tool_request(self):
+ def test_call_tool_request(self) -> None:
"""Test CallToolRequest creation."""
params = CallToolRequestParams(name="test_tool", arguments={"input": "test value"})
@@ -267,7 +268,7 @@ class TestTools:
assert request.params.name == "test_tool"
assert request.params.arguments == {"input": "test value"}
- def test_call_tool_result(self):
+ def test_call_tool_result(self) -> None:
"""Test CallToolResult creation."""
result = CallToolResult(
content=[TextContent(type="text", text="Tool executed successfully")],
@@ -280,12 +281,12 @@ class TestTools:
assert result.structuredContent == {"status": "success", "data": "test"}
assert result.isError is False
- def test_list_tools_request(self):
+ def test_list_tools_request(self) -> None:
"""Test ListToolsRequest creation."""
request = ListToolsRequest()
assert request.method == "tools/list"
- def test_list_tools_result(self):
+ def test_list_tools_result(self) -> None:
"""Test ListToolsResult creation."""
tool1 = Tool(name="tool1", inputSchema={})
tool2 = Tool(name="tool2", inputSchema={})
@@ -300,7 +301,7 @@ class TestTools:
class TestContent:
"""Test content types."""
- def test_text_content(self):
+ def test_text_content(self) -> None:
"""Test TextContent creation."""
annotations = Annotations(audience=["user"], priority=0.8)
content = TextContent(type="text", text="Hello, world!", annotations=annotations)
@@ -310,7 +311,7 @@ class TestContent:
assert content.annotations is not None
assert content.annotations.priority == 0.8
- def test_image_content(self):
+ def test_image_content(self) -> None:
"""Test ImageContent creation."""
content = ImageContent(type="image", data="base64encodeddata", mimeType="image/png")
@@ -322,7 +323,7 @@ class TestContent:
class TestOAuth:
"""Test OAuth-related types."""
- def test_oauth_client_metadata(self):
+ def test_oauth_client_metadata(self) -> None:
"""Test OAuthClientMetadata creation."""
metadata = OAuthClientMetadata(
client_name="Test Client",
@@ -336,23 +337,24 @@ class TestOAuth:
assert metadata.client_name == "Test Client"
assert len(metadata.redirect_uris) == 1
+ assert metadata.grant_types is not None
assert "authorization_code" in metadata.grant_types
- def test_oauth_client_information(self):
+ def test_oauth_client_information(self) -> None:
"""Test OAuthClientInformation creation."""
info = OAuthClientInformation(client_id="test-client-id", client_secret="test-secret")
assert info.client_id == "test-client-id"
assert info.client_secret == "test-secret"
- def test_oauth_client_information_without_secret(self):
+ def test_oauth_client_information_without_secret(self) -> None:
"""Test OAuthClientInformation without secret."""
info = OAuthClientInformation(client_id="public-client")
assert info.client_id == "public-client"
assert info.client_secret is None
- def test_oauth_tokens(self):
+ def test_oauth_tokens(self) -> None:
"""Test OAuthTokens creation."""
tokens = OAuthTokens(
access_token="access-token-123",
@@ -368,7 +370,7 @@ class TestOAuth:
assert tokens.refresh_token == "refresh-token-456"
assert tokens.scope == "read write"
- def test_oauth_metadata(self):
+ def test_oauth_metadata(self) -> None:
"""Test OAuthMetadata creation."""
metadata = OAuthMetadata(
authorization_endpoint="https://auth.example.com/authorize",
@@ -381,13 +383,14 @@ class TestOAuth:
assert metadata.authorization_endpoint == "https://auth.example.com/authorize"
assert "code" in metadata.response_types_supported
+ assert metadata.code_challenge_methods_supported is not None
assert "S256" in metadata.code_challenge_methods_supported
class TestNotifications:
"""Test notification types."""
- def test_progress_notification(self):
+ def test_progress_notification(self) -> None:
"""Test ProgressNotification creation."""
params = ProgressNotificationParams(
progressToken="progress-123", progress=50.0, total=100.0, message="Processing... 50%"
@@ -401,7 +404,7 @@ class TestNotifications:
assert notification.params.total == 100.0
assert notification.params.message == "Processing... 50%"
- def test_ping_request(self):
+ def test_ping_request(self) -> None:
"""Test PingRequest creation."""
request = PingRequest()
assert request.method == "ping"
@@ -411,24 +414,24 @@ class TestNotifications:
class TestCompletion:
"""Test completion-related types."""
- def test_completion_context(self):
+ def test_completion_context(self) -> None:
"""Test CompletionContext creation."""
context = CompletionContext(arguments={"template_var": "value"})
assert context.arguments == {"template_var": "value"}
- def test_resource_template_reference(self):
+ def test_resource_template_reference(self) -> None:
"""Test ResourceTemplateReference creation."""
ref = ResourceTemplateReference(type="ref/resource", uri="file:///path/to/{filename}")
assert ref.type == "ref/resource"
assert ref.uri == "file:///path/to/{filename}"
- def test_prompt_reference(self):
+ def test_prompt_reference(self) -> None:
"""Test PromptReference creation."""
ref = PromptReference(type="ref/prompt", name="test_prompt")
assert ref.type == "ref/prompt"
assert ref.name == "test_prompt"
- def test_complete_request(self):
+ def test_complete_request(self) -> None:
"""Test CompleteRequest creation."""
ref = PromptReference(type="ref/prompt", name="test_prompt")
arg = CompletionArgument(name="arg1", value="val")
@@ -441,7 +444,7 @@ class TestCompletion:
assert request.params.ref.name == "test_prompt" # type: ignore
assert request.params.argument.name == "arg1"
- def test_complete_result(self):
+ def test_complete_result(self) -> None:
"""Test CompleteResult creation."""
completion = Completion(values=["option1", "option2", "option3"], total=10, hasMore=True)
@@ -455,16 +458,16 @@ class TestCompletion:
class TestValidation:
"""Test validation of various types."""
- def test_invalid_jsonrpc_version(self):
+ def test_invalid_jsonrpc_version(self) -> None:
"""Test invalid JSON-RPC version validation."""
with pytest.raises(ValidationError):
JSONRPCRequest(
- jsonrpc="1.0", # Invalid version
+ jsonrpc="1.0", # pyrefly: ignore[bad-argument-type]
id=1,
method="test",
)
- def test_tool_annotations_validation(self):
+ def test_tool_annotations_validation(self) -> None:
"""Test ToolAnnotations with invalid values."""
# Valid annotations
annotations = ToolAnnotations(
@@ -472,7 +475,7 @@ class TestValidation:
)
assert annotations.title == "Test"
- def test_extra_fields_allowed(self):
+ def test_extra_fields_allowed(self) -> None:
"""Test that extra fields are allowed in models."""
# Most models should allow extra fields
tool = Tool(
@@ -482,7 +485,7 @@ class TestValidation:
)
assert tool.customField == "allowed" # type: ignore
- def test_result_meta_alias(self):
+ def test_result_meta_alias(self) -> None:
"""Test Result model with _meta alias."""
# Create with the field name (not alias)
result = Result(_meta={"key": "value"})
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index 90112bf29a3..35fbcb5dda7 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -372,10 +372,7 @@ project-excludes = [
"core/mcp/server/test_streamable_http.py",
"core/mcp/session/test_base_session.py",
"core/mcp/session/test_client_session.py",
- "core/mcp/test_auth_client_inheritance.py",
- "core/mcp/test_entities.py",
"core/mcp/test_mcp_client.py",
- "core/mcp/test_types.py",
"core/mcp/test_utils.py",
"core/memory/test_token_buffer_memory.py",
"core/model_runtime/test_model_provider_factory.py",