99"""
1010
1111import typing
12- from typing import Any , Dict , Tuple
12+ from typing import Any , cast
1313
1414import anyio
1515import anyio .abc
1616import anyio .streams .memory
1717from httpx ._models import Request , Response
1818from httpx ._transports .base import AsyncBaseTransport
1919from httpx ._types import AsyncByteStream
20+ from starlette .types import ASGIApp , Receive , Scope , Send
2021
2122
2223class StreamingASGITransport (AsyncBaseTransport ):
@@ -42,11 +43,11 @@ class StreamingASGITransport(AsyncBaseTransport):
4243
4344 def __init__ (
4445 self ,
45- app : typing . Callable ,
46+ app : ASGIApp ,
4647 task_group : anyio .abc .TaskGroup ,
4748 raise_app_exceptions : bool = True ,
4849 root_path : str = "" ,
49- client : Tuple [str , int ] = ("127.0.0.1" , 123 ),
50+ client : tuple [str , int ] = ("127.0.0.1" , 123 ),
5051 ) -> None :
5152 self .app = app
5253 self .raise_app_exceptions = raise_app_exceptions
@@ -88,13 +89,15 @@ async def handle_async_request(
8889 initial_response_ready = anyio .Event ()
8990
9091 # Synchronization for streaming response
91- asgi_send_channel , asgi_receive_channel = anyio .create_memory_object_stream (100 )
92+ asgi_send_channel , asgi_receive_channel = anyio .create_memory_object_stream [
93+ dict [str , Any ]
94+ ](100 )
9295 content_send_channel , content_receive_channel = (
9396 anyio .create_memory_object_stream [bytes ](100 )
9497 )
9598
9699 # ASGI callables.
97- async def receive () -> Dict [str , Any ]:
100+ async def receive () -> dict [str , Any ]:
98101 nonlocal request_complete
99102
100103 if request_complete :
@@ -108,15 +111,18 @@ async def receive() -> Dict[str, Any]:
108111 return {"type" : "http.request" , "body" : b"" , "more_body" : False }
109112 return {"type" : "http.request" , "body" : body , "more_body" : True }
110113
111- async def send (message : Dict [str , Any ]) -> None :
114+ async def send (message : dict [str , Any ]) -> None :
112115 nonlocal status_code , response_headers , response_started
113116
114117 await asgi_send_channel .send (message )
115118
116119 # Start the ASGI application in a separate task
117120 async def run_app () -> None :
118121 try :
119- await self .app (scope , receive , send )
122+ # Cast the receive and send functions to the ASGI types
123+ await self .app (
124+ cast (Scope , scope ), cast (Receive , receive ), cast (Send , send )
125+ )
120126 except Exception :
121127 if self .raise_app_exceptions :
122128 raise
0 commit comments