55from asyncio import Queue as AsyncQueue
66from typing import Optional , Type , Any , List , Tuple , Dict
77
8- from tornado .web import Application , StaticFileHandler , RedirectHandler
8+ from tornado .web import Application , StaticFileHandler , RedirectHandler , RequestHandler
99from tornado .websocket import WebSocketHandler
1010from tornado .platform .asyncio import AsyncIOMainLoop
1111from typing_extensions import TypedDict
1818from .base import AbstractRenderServer
1919
2020
21- class Config (TypedDict ):
21+ _RouteHandlerSpecs = List [Tuple [str , Type [RequestHandler ], Any ]]
22+
23+
24+ class Config (TypedDict , total = False ):
2225 """Render server config for :class:`TornadoRenderServer` subclasses"""
2326
2427 base_url : str
@@ -31,7 +34,7 @@ class TornadoRenderServer(AbstractRenderServer[Application, Config]):
3134
3235 _model_stream_handler_type : Type [WebSocketHandler ]
3336
34- def stop (self ):
37+ def stop (self ) -> None :
3538 try :
3639 loop = self ._loop
3740 except AttributeError : # pragma: no cover
@@ -42,14 +45,14 @@ def stop(self):
4245 loop .call_soon_threadsafe (self ._loop .stop )
4346
4447 def _create_config (self , config : Optional [Config ]) -> Config :
45- return Config (
46- {
47- "base_url " : "" ,
48- "serve_static_files " : True ,
49- "redirect_root_to_index" : True ,
50- ** ( config or {}),
51- }
52- )
48+ new_config : Config = {
49+ "base_url" : "" ,
50+ "serve_static_files " : True ,
51+ "redirect_root_to_index " : True ,
52+ }
53+ if config is not None :
54+ new_config . update ( config )
55+ return new_config
5356
5457 def _default_application (self , config : Config ) -> Application :
5558 return Application ()
@@ -73,8 +76,8 @@ def _setup_application_did_start_event(
7376 ) -> None :
7477 pass
7578
76- def _create_route_handlers (self , config : Config ) -> List [ Tuple [ Any , ...]] :
77- handlers = [
79+ def _create_route_handlers (self , config : Config ) -> _RouteHandlerSpecs :
80+ handlers : _RouteHandlerSpecs = [
7881 (
7982 "/stream" ,
8083 self ._model_stream_handler_type ,
@@ -119,18 +122,18 @@ def _run_application_in_thread(
119122 self ._run_application (config , app , host , port , args , kwargs )
120123
121124
122- class PerClientStateModelStreamHandler (WebSocketHandler ):
125+ class PerClientStateModelStreamHandler (WebSocketHandler ): # type: ignore
123126 """A web-socket handler that serves up a new model stream to each new client"""
124127
125128 _dispatcher_type : Type [AbstractDispatcher ] = SingleViewDispatcher
126129 _dispatcher_inst : AbstractDispatcher
127- _message_queue : AsyncQueue
130+ _message_queue : " AsyncQueue[str]"
128131
129132 def initialize (self , component_constructor : ComponentConstructor ) -> None :
130133 self ._component_constructor = component_constructor
131134
132- async def open (self ):
133- message_queue = AsyncQueue ()
135+ async def open (self ) -> None :
136+ message_queue : "AsyncQueue[str]" = AsyncQueue ()
134137 query_params = {k : v [0 ].decode () for k , v in self .request .arguments .items ()}
135138 dispatcher = self ._dispatcher_type (
136139 Layout (self ._component_constructor (** query_params ))
@@ -154,7 +157,7 @@ async def run() -> None:
154157 async def on_message (self , message : str ) -> None :
155158 await self ._message_queue .put (message )
156159
157- def on_close (self ):
160+ def on_close (self ) -> None :
158161 asyncio .ensure_future (self ._dispatcher_inst .__aexit__ (None , None , None ))
159162
160163
0 commit comments