Skip to content

Commit c8fe76c

Browse files
committed
make run_in_thread return a Thread
ParamSpec is not fully supported in mypy yet though. Hence, the type: ignore comment.
1 parent 18fd2e9 commit c8fe76c

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

requirements/pkg-deps.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
typing-extensions >=3.7.4
1+
typing-extensions >=3.10
22
mypy-extensions >=0.4.3
33
anyio >=3.0
44
jsonpatch >=1.26

src/idom/server/proto.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from threading import Thread
34
from typing import Optional, TypeVar
45

56
from typing_extensions import Protocol
@@ -32,7 +33,7 @@ class Server(Protocol[_App]):
3233
def run(self, host: str, port: int) -> None:
3334
"""Start running the server"""
3435

35-
def run_in_thread(self, host: str, port: int) -> None:
36+
def run_in_thread(self, host: str, port: int) -> Thread:
3637
"""Run the server in a thread"""
3738

3839
def wait_until_started(self, timeout: Optional[float] = None) -> None:

src/idom/server/utils.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from pathlib import Path
77
from socket import socket
88
from threading import Event, Thread
9-
from typing import Any, Callable, List, Optional, TypeVar, cast
9+
from typing import Any, Callable, List, Optional
10+
11+
from typing_extensions import ParamSpec
1012

1113
import idom
1214

@@ -22,21 +24,23 @@
2224
"tornado",
2325
]
2426

25-
_Func = TypeVar("_Func", bound=Callable[..., None])
27+
28+
_FuncParams = ParamSpec("_FuncParams")
2629

2730

28-
def threaded(function: _Func) -> _Func:
31+
def threaded(function: Callable[_FuncParams, None]) -> Callable[_FuncParams, Thread]: # type: ignore
2932
@wraps(function)
30-
def wrapper(*args: Any, **kwargs: Any) -> None:
33+
def wrapper(*args: Any, **kwargs: Any) -> Thread:
3134
def target() -> None:
3235
asyncio.set_event_loop(asyncio.new_event_loop())
3336
function(*args, **kwargs)
3437

35-
Thread(target=target, daemon=True).start()
38+
thread = Thread(target=target, daemon=True)
39+
thread.start()
3640

37-
return None
41+
return thread
3842

39-
return cast(_Func, wrapper)
43+
return wrapper
4044

4145

4246
def wait_on_event(description: str, event: Event, timeout: Optional[float]) -> None:

0 commit comments

Comments
 (0)