|
5 | 5 |
|
6 | 6 | from abc import ABC, abstractmethod |
7 | 7 | from collections.abc import AsyncGenerator |
8 | | -from typing import Any |
| 8 | +from typing import Any, TYPE_CHECKING |
9 | 9 |
|
10 | 10 | from pydantic import ValidationError |
11 | 11 | from sse_starlette.sse import EventSourceResponse |
|
44 | 44 |
|
45 | 45 | logger = logging.getLogger(__name__) |
46 | 46 |
|
47 | | -try: |
| 47 | +if TYPE_CHECKING: |
48 | 48 | from fastapi import FastAPI |
49 | 49 | from sse_starlette.sse import EventSourceResponse |
50 | 50 | from starlette.applications import Starlette |
|
53 | 53 | from starlette.responses import JSONResponse, Response |
54 | 54 |
|
55 | 55 | _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 |
69 | 77 |
|
70 | 78 |
|
71 | 79 | class StarletteUserProxy(A2AUser): |
|
0 commit comments