Skip to content

Commit 17766ae

Browse files
committed
Fix mypy errors
1 parent fe26fa1 commit 17766ae

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

src/a2a/server/apps/jsonrpc/fastapi_app.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import logging
22

3-
from typing import Any
3+
from typing import TYPE_CHECKING, Any
44

55

6-
try:
6+
if TYPE_CHECKING:
77
from fastapi import FastAPI, Request, Response
8-
except ImportError:
9-
FastAPI = object
10-
Request = object
11-
Response = object
8+
else:
9+
try:
10+
from fastapi import FastAPI, Request, Response
11+
except ImportError:
12+
FastAPI = Any
13+
Request = Any
14+
Response = Any
1215

1316
from a2a.server.apps.jsonrpc.jsonrpc_app import (
1417
CallContextBuilder,

src/a2a/server/apps/jsonrpc/jsonrpc_app.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from abc import ABC, abstractmethod
77
from collections.abc import AsyncGenerator
8-
from typing import Any
8+
from typing import Any, TYPE_CHECKING
99

1010
from pydantic import ValidationError
1111
from sse_starlette.sse import EventSourceResponse
@@ -44,7 +44,7 @@
4444

4545
logger = logging.getLogger(__name__)
4646

47-
try:
47+
if TYPE_CHECKING:
4848
from fastapi import FastAPI
4949
from sse_starlette.sse import EventSourceResponse
5050
from starlette.applications import Starlette
@@ -53,19 +53,27 @@
5353
from starlette.responses import JSONResponse, Response
5454

5555
_http_server_installed = True
56-
except ImportError:
57-
_http_server_installed = False
58-
# Define placeholder types for type hinting and to avoid import errors in other files.
59-
# These will not be used at runtime if deps are missing, as __init__ will raise.
60-
(
61-
FastAPI,
62-
EventSourceResponse,
63-
Starlette,
64-
BaseUser,
65-
Request,
66-
JSONResponse,
67-
Response,
68-
) = (object,) * 7
56+
else:
57+
try:
58+
from fastapi import FastAPI
59+
from sse_starlette.sse import EventSourceResponse
60+
from starlette.applications import Starlette
61+
from starlette.authentication import BaseUser
62+
from starlette.requests import Request
63+
from starlette.responses import JSONResponse, Response
64+
65+
_http_server_installed = True
66+
except ImportError:
67+
_http_server_installed = False
68+
# Provide placeholder types for runtime type hinting when dependencies are not installed.
69+
# These will not be used if the code path that needs them is guarded by _http_server_installed.
70+
FastAPI = Any
71+
EventSourceResponse = Any
72+
Starlette = Any
73+
BaseUser = Any
74+
Request = Any
75+
JSONResponse = Any
76+
Response = Any
6977

7078

7179
class StarletteUserProxy(A2AUser):

src/a2a/server/apps/jsonrpc/starlette_app.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import logging
22

3-
from typing import Any
3+
from typing import TYPE_CHECKING, Any
44

55

6-
try:
6+
if TYPE_CHECKING:
77
from starlette.applications import Starlette
88
from starlette.routing import Route
9-
except ImportError:
10-
Starlette = object
11-
Route = object
9+
else:
10+
try:
11+
from starlette.applications import Starlette
12+
from starlette.routing import Route
13+
except ImportError:
14+
Starlette = Any
15+
Route = Any
1216

1317
from a2a.server.apps.jsonrpc.jsonrpc_app import (
1418
CallContextBuilder,

0 commit comments

Comments
 (0)