1- from asyncio import create_task , Future
1+ from asyncio import create_task , Future , Task
22from collections .abc import Awaitable , Callable
33from dataclasses import dataclass
44from typing import Optional , Union
@@ -36,25 +36,26 @@ def __init__(
3636 self .send_cb = send_cb
3737 self .context = context
3838 self .task_holder = task_holder
39- self ._close_callback = None
39+ self ._close_callback : Callable [[], None ] | None = None
4040
4141 # Various tornado APIs (e.g. RequestHandler.flush()) return a Future which
4242 # application code does not need to await. The operations these represent
4343 # are expected to complete even if the Future is discarded. ASGI is based
4444 # on 'awaitable callables', which do not guarantee this. So we need to hold
4545 # references to tasks until they complete
46- def _bg_task (self , coro ):
46+ def _bg_task (self , coro ) -> Future : # type: ignore
4747 task = create_task (coro )
4848 self .task_holder .add (task )
4949 task .add_done_callback (self .task_holder .discard )
5050 return task
5151
5252 async def _write_headers (
5353 self ,
54- start_line : ResponseStartLine ,
54+ start_line : Union [ "RequestStartLine" , " ResponseStartLine" ] ,
5555 headers : HTTPHeaders ,
5656 chunk : Optional [bytes ] = None ,
57- ):
57+ ) -> None :
58+ assert isinstance (start_line , ResponseStartLine )
5859 await self .send_cb (
5960 {
6061 "type" : "http.response.start" ,
@@ -76,7 +77,7 @@ def write_headers(
7677 ) -> "Future[None]" :
7778 return self ._bg_task (self ._write_headers (start_line , headers , chunk ))
7879
79- async def _write (self , chunk : bytes ):
80+ async def _write (self , chunk : bytes ) -> None :
8081 await self .send_cb (
8182 {"type" : "http.response.body" , "body" : chunk , "more_body" : True }
8283 )
@@ -94,7 +95,7 @@ def finish(self) -> None:
9495 )
9596 )
9697
97- def set_close_callback (self , callback : Optional [Callable [[], None ]]):
98+ def set_close_callback (self , callback : Optional [Callable [[], None ]]) -> None :
9899 self ._close_callback = callback
99100
100101 def _on_connection_close (self ) -> None :
@@ -109,14 +110,18 @@ class ASGIAdapter:
109110
110111 def __init__ (self , application : Application ):
111112 self .application = application
112- self .task_holder = set ()
113+ self .task_holder : set [ Task ] = set ()
113114
114- async def __call__ (self , scope , receive : ReceiveCallable , send : SendCallable ):
115+ async def __call__ (
116+ self , scope : dict , receive : ReceiveCallable , send : SendCallable
117+ ) -> None :
115118 if scope ["type" ] == "http" :
116119 return await self .http_scope (scope , receive , send )
117120 raise KeyError (scope ["type" ])
118121
119- async def http_scope (self , scope , receive : ReceiveCallable , send : SendCallable ):
122+ async def http_scope (
123+ self , scope : dict , receive : ReceiveCallable , send : SendCallable
124+ ) -> None :
120125 """Handles one HTTP request"""
121126 ctx = ASGIHTTPRequestContext (scope ["scheme" ])
122127 if client_addr := scope .get ("client" , None ):
0 commit comments