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.

chore: clean some isinstance (#37602)

Asuka Minatod5cdb2e6f1f8
diff --git a/api/controllers/openapi/auth/surface_gate.py b/api/controllers/openapi/auth/surface_gate.py
index 49485fb28c9..f3d2d0735bb 100644
--- a/api/controllers/openapi/auth/surface_gate.py
+++ b/api/controllers/openapi/auth/surface_gate.py
@@ -74,12 +74,13 @@ def accept_subjects(*accepted: SubjectType) -> Callable[[F], F]:
def _coerce_subject_type(raw: object) -> SubjectType | None:
- if raw is None:
- return None
- if isinstance(raw, SubjectType):
- return raw
- if isinstance(raw, str):
- return SubjectType(raw)
+ match raw:
+ case None:
+ return None
+ case SubjectType():
+ return raw
+ case str():
+ return SubjectType(raw)
return None
diff --git a/api/core/mcp/client/streamable_client.py b/api/core/mcp/client/streamable_client.py
index acba3e666b1..39dc676627e 100644
--- a/api/core/mcp/client/streamable_client.py
+++ b/api/core/mcp/client/streamable_client.py
@@ -14,6 +14,7 @@ from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from dataclasses import dataclass
from datetime import timedelta
+from http import HTTPStatus
from typing import Any, cast
import httpx
@@ -293,28 +294,27 @@ class StreamableHTTPTransport:
json=message.model_dump(by_alias=True, mode="json", exclude_none=True),
headers=headers,
) as response:
- if response.status_code == 202:
- logger.debug("Received 202 Accepted")
- return
-
- if response.status_code == 204:
- logger.debug("Received 204 No Content")
- return
-
- if response.status_code == 404:
- if isinstance(message.root, JSONRPCRequest):
- error_msg = (
- f"MCP server URL returned 404 Not Found: {self.url} "
- "— verify the server URL is correct and the server is running"
- if is_initialization
- else "Session terminated by server"
- )
- self._send_session_terminated_error(
- ctx.server_to_client_queue,
- message.root.id,
- message=error_msg,
- )
- return
+ match response.status_code:
+ case HTTPStatus.ACCEPTED:
+ logger.debug("Received 202 Accepted")
+ return
+ case HTTPStatus.NO_CONTENT:
+ logger.debug("Received 204 No Content")
+ return
+ case HTTPStatus.NOT_FOUND:
+ if isinstance(message.root, JSONRPCRequest):
+ error_msg = (
+ f"MCP server URL returned 404 Not Found: {self.url} "
+ "— verify the server URL is correct and the server is running"
+ if is_initialization
+ else "Session terminated by server"
+ )
+ self._send_session_terminated_error(
+ ctx.server_to_client_queue,
+ message.root.id,
+ message=error_msg,
+ )
+ return
response.raise_for_status()
if is_initialization:
diff --git a/api/core/tools/custom_tool/tool.py b/api/core/tools/custom_tool/tool.py
index 2e618b7ea51..3edb04d7b94 100644
--- a/api/core/tools/custom_tool/tool.py
+++ b/api/core/tools/custom_tool/tool.py
@@ -359,15 +359,16 @@ class ApiTool(Tool):
if value is None:
return None
elif property["type"] == "object" or property["type"] == "array":
- if isinstance(value, str):
- try:
- return json.loads(value)
- except ValueError:
+ match value:
+ case str():
+ try:
+ return json.loads(value)
+ except ValueError:
+ return value
+ case dict():
+ return value
+ case _:
return value
- elif isinstance(value, dict):
- return value
- else:
- return value
else:
raise ValueError(f"Invalid type {property['type']} for property {property}")
elif "anyOf" in property and isinstance(property["anyOf"], list):