Skip to content

Commit 4643519

Browse files
committed
fix: use TYPE_CHECKING to properly handle MCP imports for mypy
- Use TYPE_CHECKING block to declare types for mypy - Declare MCP_AVAILABLE and INTERNAL_ERROR as type stubs - Remove unused type: ignore comments - This properly handles the case where MCP is not installed
1 parent 3c89d8c commit 4643519

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

examples/mcp_http_error_handling_demo.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,28 @@
1313

1414
import asyncio
1515
import json
16-
from typing import Any
16+
from typing import TYPE_CHECKING, Any
1717

1818
from agents import Agent, Runner, function_tool
1919

2020
# Import MCP types for proper error handling
21-
try:
21+
if TYPE_CHECKING:
2222
from mcp.shared.exceptions import McpError # type: ignore[import-not-found]
23-
from mcp.types import INTERNAL_ERROR, ErrorData # type: ignore[import-not-found]
23+
from mcp.types import ErrorData # type: ignore[import-not-found]
24+
25+
MCP_AVAILABLE: bool
26+
INTERNAL_ERROR: int
27+
28+
try:
29+
from mcp.shared.exceptions import McpError
30+
from mcp.types import INTERNAL_ERROR, ErrorData # type: ignore[no-redef]
2431

2532
MCP_AVAILABLE = True
2633
except ImportError:
2734
# Fallback for Python < 3.10 or when MCP is not installed
2835
MCP_AVAILABLE = False
2936
McpError = Exception
30-
ErrorData = None
37+
ErrorData = type("ErrorData", (), {})
3138
INTERNAL_ERROR = -32603
3239

3340

0 commit comments

Comments
 (0)