Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
rmorshea committed Sep 26, 2020
1 parent 14e0264 commit 953b8c7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion idom/server/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def stop(self) -> None:
self.loop.call_soon_threadsafe(self._stop)

@abc.abstractmethod
def _stop(self):
def _stop(self) -> None:
raise NotImplementedError()

@abc.abstractmethod
Expand Down
10 changes: 5 additions & 5 deletions idom/server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@
_S = TypeVar("_S", bound=AbstractRenderServer[Any, Any])


def _find_default_server_type() -> Optional[Type[AbstractRenderServer[Any, Any]]]:
def _find_default_server_type() -> Optional[Type[_S]]:
for name in ["sanic.PerClientStateServer"]:
module_name, server_name = name.split(".")
try:
module = import_module(f"idom.server.{module_name}")
except ImportError: # pragma: no cover
pass
else:
return getattr(module, server_name)
return cast(Type[_S], getattr(module, server_name))
else: # pragma: no cover
return None


def run(
element: ElementConstructor,
server_type: Optional[Type[_S]] = _find_default_server_type(),
host: Optional[str] = "127.0.0.1",
host: str = "127.0.0.1",
port: Optional[int] = None,
server_options: Optional[Any] = None,
run_options: Optional[Dict[str, Any]] = None,
Expand Down Expand Up @@ -68,7 +68,7 @@ def run(

def multiview_server(
server_type: Type[_S],
host: Optional[str] = "127.0.0.1",
host: str = "127.0.0.1",
port: Optional[int] = None,
server_options: Optional[Any] = None,
run_options: Optional[Dict[str, Any]] = None,
Expand Down Expand Up @@ -110,7 +110,7 @@ def multiview_server(

def hotswap_server(
server_type: Type[_S],
host: Optional[str] = "127.0.0.1",
host: str = "127.0.0.1",
port: Optional[int] = None,
server_options: Optional[Any] = None,
run_options: Optional[Dict[str, Any]] = None,
Expand Down
32 changes: 15 additions & 17 deletions idom/widgets/utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
from typing import Any, Callable, Tuple, Dict, Set, cast

from typing_extensions import Protocol
from typing import TypeVar, Any, Callable, Tuple, Dict, Set

from idom.core import hooks
from idom.core.element import ElementConstructor, element
from idom.utils import Ref


class MountFunc(Protocol):
"""Function for mounting views"""

def __call__(
self, constructor: ElementConstructor, *args: Any, **kwargs: Any
) -> Any:
...
MountFunc = Callable[[ElementConstructor], None]


def hotswap(shared: bool = False) -> Tuple[MountFunc, ElementConstructor]:
Expand Down Expand Up @@ -82,16 +74,22 @@ def swap(constructor: Callable[[], Any]) -> None:
def HotSwap() -> Any:
return constructor_ref.current()

def swap(constructor: ElementConstructor) -> None:
def swap(constructor: Callable[[], Any]) -> None:
constructor_ref.current = constructor
return None

return cast(MountFunc, swap), HotSwap
return swap, HotSwap


_Func = Callable[..., Any]
_FuncVar = TypeVar("_FuncVar", bound=_Func)


def _use_callable(initial_func):
func, _set_func = hooks.use_state(lambda: initial_func)
return func, lambda new: _set_func(lambda old: new)
def _use_callable(initial_func: _FuncVar) -> Tuple[_FuncVar, Callable[[_Func], None]]:
state: Tuple[_FuncVar, Callable[[_Func], None]] = hooks.use_state(
lambda: initial_func
)
return state[0], lambda new: state[1](lambda old: new)


def multiview() -> Tuple["MultiViewMount", ElementConstructor]:
Expand Down Expand Up @@ -148,7 +146,7 @@ def __init__(self, views: Dict[str, ElementConstructor]):
def remove(self, view_id: str) -> None:
del self._views[view_id]

def __getitem__(self, view_id: str) -> MountFunc:
def __getitem__(self, view_id: str) -> Callable[[ElementConstructor], str]:
def mount(constructor: ElementConstructor) -> str:
self._views[view_id] = constructor
return view_id
Expand All @@ -161,5 +159,5 @@ def __call__(self, constructor: ElementConstructor) -> str:
self._views[view_id] = constructor
return view_id

def __repr__(self):
def __repr__(self) -> str:
return f"{type(self).__name__}({self._views})"

0 comments on commit 953b8c7

Please sign in to comment.