Skip to content

Commit 64d0f1e

Browse files
committed
fix cov
1 parent 99304d8 commit 64d0f1e

File tree

3 files changed

+26
-40
lines changed

3 files changed

+26
-40
lines changed

src/idom/server/flask.py

+7-25
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from queue import Queue as ThreadQueue
1010
from threading import Event as ThreadEvent
1111
from threading import Thread
12-
from typing import Any, Callable, Dict, NamedTuple, Optional, Union, cast
12+
from typing import Any, Callable, Dict, NamedTuple, NoReturn, Optional, Union, cast
1313

1414
from flask import (
1515
Blueprint,
@@ -84,13 +84,12 @@ async def serve_development_app(
8484

8585
server: Ref[BaseWSGIServer] = Ref()
8686

87-
def run_server() -> None: # pragma: no cover
88-
# we don't cover this function because coverage doesn't work right in threads
87+
def run_server() -> None:
8988
server.current = make_server(host, port, app, threaded=True)
9089
if started:
9190
loop.call_soon_threadsafe(started.set)
9291
try:
93-
server.current.serve_forever()
92+
server.current.serve_forever() # type: ignore
9493
finally:
9594
loop.call_soon_threadsafe(stopped.set)
9695

@@ -178,12 +177,8 @@ def model_stream(ws: WebSocket, path: str = "") -> None:
178177
def send(value: Any) -> None:
179178
ws.send(json.dumps(value))
180179

181-
def recv() -> Optional[LayoutEvent]:
182-
event = ws.receive()
183-
if event is not None:
184-
return LayoutEvent(**json.loads(event))
185-
else:
186-
return None
180+
def recv() -> LayoutEvent:
181+
return LayoutEvent(**json.loads(ws.receive()))
187182

188183
dispatch_in_thread(ws, path, constructor(), send, recv)
189184

@@ -197,7 +192,7 @@ def dispatch_in_thread(
197192
component: ComponentType,
198193
send: Callable[[Any], None],
199194
recv: Callable[[], Optional[LayoutEvent]],
200-
) -> None:
195+
) -> NoReturn:
201196
dispatch_thread_info_created = ThreadEvent()
202197
dispatch_thread_info_ref: idom.Ref[Optional[_DispatcherThreadInfo]] = idom.Ref(None)
203198

@@ -255,21 +250,14 @@ def run_send() -> None:
255250
try:
256251
while True:
257252
value = recv()
258-
if value is None:
259-
stop.set()
260-
break
261-
# BUG: https://github.com/nedbat/coveragepy/issues/1012
262-
# Coverage isn't able to support concurrency coverage for both threading and gevent
263-
dispatch_thread_info.dispatch_loop.call_soon_threadsafe( # pragma: no cover
253+
dispatch_thread_info.dispatch_loop.call_soon_threadsafe(
264254
dispatch_thread_info.async_recv_queue.put_nowait, value
265255
)
266256
finally:
267257
dispatch_thread_info.dispatch_loop.call_soon_threadsafe(
268258
dispatch_thread_info.dispatch_future.cancel
269259
)
270260

271-
return None
272-
273261

274262
@dataclass
275263
class Connection:
@@ -290,9 +278,3 @@ class _DispatcherThreadInfo(NamedTuple):
290278
dispatch_future: "asyncio.Future[Any]"
291279
thread_send_queue: "ThreadQueue[LayoutUpdate]"
292280
async_recv_queue: "AsyncQueue[LayoutEvent]"
293-
294-
295-
def _join_url_paths(*args: str) -> str:
296-
# urllib.parse.urljoin performs more logic than is needed. Thus we need a util func
297-
# to join paths as if they were POSIX paths.
298-
return "/".join(map(lambda x: str(x).rstrip("/"), filter(None, args)))

src/idom/server/sanic.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232

3333
logger = logging.getLogger(__name__)
3434

35-
ConnectionContext: type[Context[request.Request | None]] = create_context(
36-
None, "RequestContext"
35+
ConnectionContext: type[Context[Connection | None]] = create_context(
36+
None, "ConnectionContext"
3737
)
3838

3939

@@ -69,12 +69,12 @@ async def serve_development_app(
6969

7070
def use_connection() -> Connection:
7171
"""Get the current :class:`Connection`"""
72-
request = use_context(ConnectionContext)
73-
if request is None:
72+
connection = use_context(ConnectionContext)
73+
if connection is None:
7474
raise RuntimeError( # pragma: no cover
75-
"No request. Are you running with a Sanic server?"
75+
"No connection. Are you running with a Sanic server?"
7676
)
77-
return request
77+
return connection
7878

7979

8080
def use_scope() -> ASGIScope:
@@ -112,17 +112,17 @@ def _setup_common_routes(blueprint: Blueprint, options: Options) -> None:
112112

113113
if options.serve_static_files:
114114

115-
@blueprint.route("/")
116-
@blueprint.route("/<path:path>")
117115
async def single_page_app_files(
118-
request: request.Request, path: str = ""
116+
request: request.Request,
117+
path: str = "",
119118
) -> response.HTTPResponse:
120119
path = urllib_parse.unquote(path)
121120
return await response.file(CLIENT_BUILD_DIR / client_build_dir_path(path))
122121

123-
@blueprint.route("/_api/modules/<path:path>")
124-
@blueprint.route("/<_:path>/_api/modules/<path:path>")
125-
async def single_page_app_files(
122+
blueprint.add_route(single_page_app_files, "/")
123+
blueprint.add_route(single_page_app_files, "/<path:path>")
124+
125+
async def web_module_files(
126126
request: request.Request,
127127
path: str,
128128
_: str = "", # this is not used
@@ -131,12 +131,13 @@ async def single_page_app_files(
131131
path = urllib_parse.unquote(path)
132132
return await response.file(wm_dir.joinpath(*path.split("/")))
133133

134+
blueprint.add_route(web_module_files, "/_api/modules/<path:path>")
135+
blueprint.add_route(web_module_files, "/<_:path>/_api/modules/<path:path>")
136+
134137

135138
def _setup_single_view_dispatcher_route(
136139
blueprint: Blueprint, constructor: RootComponentConstructor
137140
) -> None:
138-
@blueprint.websocket("/_api/stream")
139-
@blueprint.websocket("/<path:path>/_api/stream") # type: ignore
140141
async def model_stream(
141142
request: request.Request, socket: WebSocketCommonProtocol, path: str = ""
142143
) -> None:
@@ -148,6 +149,9 @@ async def model_stream(
148149
recv,
149150
)
150151

152+
blueprint.add_websocket_route(model_stream, "/_api/stream")
153+
blueprint.add_websocket_route(model_stream, "/<path:path>/_api/stream")
154+
151155

152156
@dataclass
153157
class Connection:

src/idom/server/tornado.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class ModelStreamHandler(WebSocketHandler):
177177
def initialize(self, component_constructor: ComponentConstructor) -> None:
178178
self._component_constructor = component_constructor
179179

180-
async def open(self, *args, **kwargs) -> None:
180+
async def open(self, *args: Any, **kwargs: Any) -> None:
181181
message_queue: "AsyncQueue[str]" = AsyncQueue()
182182

183183
async def send(value: VdomJsonPatch) -> None:

0 commit comments

Comments
 (0)