Skip to content

Commit dd28505

Browse files
committed
fix type annotations
1 parent ce8e060 commit dd28505

File tree

6 files changed

+39
-30
lines changed

6 files changed

+39
-30
lines changed

src/idom/core/dispatcher.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,26 @@ async def dispatch_single_view(
3737
task_group.start_soon(_single_incoming_loop, layout, recv)
3838

3939

40-
_SharedDispatchFuture = Callable[[SendCoroutine, RecvCoroutine], Future]
40+
_SharedViewDispatcherFuture = Callable[[SendCoroutine, RecvCoroutine], Future[None]]
41+
_SharedViewDispatcherCoro = Callable[[SendCoroutine, RecvCoroutine], Awaitable[None]]
4142

4243

4344
@asynccontextmanager
4445
async def create_shared_view_dispatcher(
4546
layout: Layout, run_forever: bool = False
46-
) -> AsyncIterator[_SharedDispatchFuture]:
47+
) -> AsyncIterator[_SharedViewDispatcherFuture]:
4748
with layout:
4849
(
4950
dispatch_shared_view,
5051
model_state,
5152
all_update_queues,
5253
) = await _make_shared_view_dispatcher(layout)
5354

54-
dispatch_tasks: List[Future] = []
55+
dispatch_tasks: List[Future[None]] = []
5556

5657
def dispatch_shared_view_soon(
5758
send: SendCoroutine, recv: RecvCoroutine
58-
) -> Future:
59+
) -> Future[None]:
5960
future = ensure_future(dispatch_shared_view(send, recv))
6061
dispatch_tasks.append(future)
6162
return future
@@ -87,10 +88,10 @@ def dispatch_shared_view_soon(
8788

8889
def ensure_shared_view_dispatcher_future(
8990
layout: Layout,
90-
) -> Tuple[Future, _SharedDispatchFuture]:
91-
dispatcher_future = Future()
91+
) -> Tuple[Future[None], _SharedViewDispatcherCoro]:
92+
dispatcher_future: Future[_SharedViewDispatcherCoro] = Future()
9293

93-
async def dispatch_shared_view_forever():
94+
async def dispatch_shared_view_forever() -> None:
9495
with layout:
9596
(
9697
dispatch_shared_view,
@@ -113,12 +114,9 @@ async def dispatch(send: SendCoroutine, recv: RecvCoroutine) -> None:
113114
return ensure_future(dispatch_shared_view_forever()), dispatch
114115

115116

116-
_SharedDispatchCoroutine = Callable[[SendCoroutine, RecvCoroutine], Awaitable[None]]
117-
118-
119117
async def _make_shared_view_dispatcher(
120118
layout: Layout,
121-
) -> Tuple[_SharedDispatchCoroutine, Ref[Any], WeakSet[Queue[LayoutUpdate]]]:
119+
) -> Tuple[_SharedViewDispatcherCoro, Ref[Any], WeakSet[Queue[LayoutUpdate]]]:
122120
initial_update = await layout.render()
123121
model_state = Ref(initial_update.apply_to({}))
124122

@@ -158,7 +156,7 @@ async def _shared_outgoing_loop(
158156

159157
async def _wait_until_first_complete(
160158
*tasks: Awaitable[Any],
161-
) -> Sequence[Future]:
159+
) -> Sequence[Future[Any]]:
162160
futures = [ensure_future(t) for t in tasks]
163161
await wait(futures, return_when=FIRST_COMPLETED)
164162
return futures

src/idom/core/layout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class LayoutEvent(NamedTuple):
4747
"""A list of event data passed to the event handler."""
4848

4949

50-
_Self = TypeVar("_Self")
50+
_Self = TypeVar("_Self", bound="Layout")
5151

5252

5353
class Layout:

src/idom/server/fastapi.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import logging
44
import sys
55
import time
6-
from asyncio.futures import Future
76
from threading import Event, Thread
8-
from typing import Any, Awaitable, Callable, Dict, Optional, Tuple, Union
7+
from typing import Any, Dict, Optional, Tuple, Union
98

109
from fastapi import APIRouter, FastAPI, Request, WebSocket
1110
from fastapi.middleware.cors import CORSMiddleware
@@ -162,6 +161,11 @@ def _run_application_in_thread(
162161
# uvicorn does the event loop setup for us
163162
self._run_application(config, app, host, port, args, kwargs)
164163

164+
async def _run_dispatcher(
165+
self, send: SendCoroutine, recv: RecvCoroutine, params: Dict[str, Any]
166+
) -> None:
167+
raise NotImplementedError()
168+
165169

166170
class PerClientStateServer(FastApiRenderServer):
167171
"""Each client view will have its own state."""
@@ -182,9 +186,6 @@ async def _run_dispatcher(
182186
class SharedClientStateServer(FastApiRenderServer):
183187
"""All connected client views will have shared state."""
184188

185-
_dispatch_daemon_future: Future
186-
_dispatch_coroutine: Callable[[SendCoroutine, RecvCoroutine], Awaitable[None]]
187-
188189
def _setup_application(self, config: Config, app: FastAPI) -> None:
189190
app.on_event("startup")(self._activate_dispatcher)
190191
app.on_event("shutdown")(self._deactivate_dispatcher)

src/idom/server/flask.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import idom
2020
from idom.config import IDOM_CLIENT_BUILD_DIR
2121
from idom.core.component import AbstractComponent
22-
from idom.core.dispatcher import RecvCoroutine, SendCoroutine, dispatch_single_view
22+
from idom.core.dispatcher import dispatch_single_view
2323
from idom.core.layout import LayoutEvent, LayoutUpdate
2424

2525
from .base import AbstractRenderServer
@@ -171,24 +171,32 @@ def _generic_run_application(
171171
self._wsgi_server.serve_forever()
172172

173173
def _run_dispatcher(
174-
self, query_params: Dict[str, Any], send: SendCoroutine, recv: RecvCoroutine
175-
):
174+
self,
175+
query_params: Dict[str, Any],
176+
send: Callable[[Any], None],
177+
recv: Callable[[], Optional[LayoutEvent]],
178+
) -> None:
176179
raise NotImplementedError()
177180

178181

179182
class PerClientStateServer(FlaskRenderServer):
180183
"""Each client view will have its own state."""
181184

182185
def _run_dispatcher(
183-
self, query_params: Dict[str, Any], send: SendCoroutine, recv: RecvCoroutine
184-
):
186+
self,
187+
query_params: Dict[str, Any],
188+
send: Callable[[Any], None],
189+
recv: Callable[[], Optional[LayoutEvent]],
190+
) -> None:
185191
dispatch_single_view_in_thread(
186192
self._root_component_constructor(**query_params), send, recv
187193
)
188194

189195

190196
def dispatch_single_view_in_thread(
191-
component: AbstractComponent, send: SendCoroutine, recv: RecvCoroutine
197+
component: AbstractComponent,
198+
send: Callable[[Any], None],
199+
recv: Callable[[], Optional[LayoutEvent]],
192200
) -> None:
193201
dispatch_thread_info_created = ThreadEvent()
194202
dispatch_thread_info_ref: idom.Ref[Optional[_DispatcherThreadInfo]] = idom.Ref(None)

src/idom/server/sanic.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
import json
33
from threading import Event
4-
from typing import Any, Awaitable, Callable, Dict, Optional, Tuple, Union
4+
from typing import Any, Dict, Optional, Tuple, Union
55

66
from mypy_extensions import TypedDict
77
from sanic import Blueprint, Sanic, request, response
@@ -108,6 +108,11 @@ def redirect_to_index(
108108
f"{blueprint.url_prefix}/client/index.html?{request.query_string}"
109109
)
110110

111+
async def _run_dispatcher(
112+
self, send: SendCoroutine, recv: RecvCoroutine, params: Dict[str, Any]
113+
) -> None:
114+
raise NotImplementedError()
115+
111116
def _run_application(
112117
self,
113118
config: Config,
@@ -182,9 +187,6 @@ async def _run_dispatcher(
182187
class SharedClientStateServer(SanicRenderServer):
183188
"""All connected client views will have shared state."""
184189

185-
_dispatch_daemon_future: asyncio.Future
186-
_dispatch_coroutine: Callable[[SendCoroutine, RecvCoroutine], Awaitable[None]]
187-
188190
def _setup_application(self, config: Config, app: Sanic) -> None:
189191
app.register_listener(self._activate_dispatcher, "before_server_start")
190192
app.register_listener(self._deactivate_dispatcher, "before_server_stop")

src/idom/server/tornado.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ def _run_application_in_thread(
129129
class PerClientStateModelStreamHandler(WebSocketHandler):
130130
"""A web-socket handler that serves up a new model stream to each new client"""
131131

132-
_dispatch_future: Future
133-
_message_queue: "AsyncQueue[str]"
132+
_dispatch_future: Future[None]
133+
_message_queue: AsyncQueue[str]
134134

135135
def initialize(self, component_constructor: ComponentConstructor) -> None:
136136
self._component_constructor = component_constructor

0 commit comments

Comments
 (0)