1- from typing import Any , Callable , Tuple , Dict , Set , cast
2-
3- from typing_extensions import Protocol
1+ from typing import TypeVar , Any , Callable , Tuple , Dict , Set
42
53from idom .core import hooks
64from idom .core .element import ElementConstructor , element
75from idom .utils import Ref
86
97
10- class MountFunc (Protocol ):
11- """Function for mounting views"""
12-
13- def __call__ (
14- self , constructor : ElementConstructor , * args : Any , ** kwargs : Any
15- ) -> Any :
16- ...
8+ MountFunc = Callable [[ElementConstructor ], None ]
179
1810
1911def hotswap (shared : bool = False ) -> Tuple [MountFunc , ElementConstructor ]:
@@ -82,16 +74,22 @@ def swap(constructor: Callable[[], Any]) -> None:
8274 def HotSwap () -> Any :
8375 return constructor_ref .current ()
8476
85- def swap (constructor : ElementConstructor ) -> None :
77+ def swap (constructor : Callable [[], Any ] ) -> None :
8678 constructor_ref .current = constructor
8779 return None
8880
89- return cast (MountFunc , swap ), HotSwap
81+ return swap , HotSwap
82+
83+
84+ _Func = Callable [..., Any ]
85+ _FuncVar = TypeVar ("_FuncVar" , bound = _Func )
9086
9187
92- def _use_callable (initial_func ):
93- func , _set_func = hooks .use_state (lambda : initial_func )
94- return func , lambda new : _set_func (lambda old : new )
88+ def _use_callable (initial_func : _FuncVar ) -> Tuple [_FuncVar , Callable [[_Func ], None ]]:
89+ state : Tuple [_FuncVar , Callable [[_Func ], None ]] = hooks .use_state (
90+ lambda : initial_func
91+ )
92+ return state [0 ], lambda new : state [1 ](lambda old : new )
9593
9694
9795def multiview () -> Tuple ["MultiViewMount" , ElementConstructor ]:
@@ -148,7 +146,7 @@ def __init__(self, views: Dict[str, ElementConstructor]):
148146 def remove (self , view_id : str ) -> None :
149147 del self ._views [view_id ]
150148
151- def __getitem__ (self , view_id : str ) -> MountFunc :
149+ def __getitem__ (self , view_id : str ) -> Callable [[ ElementConstructor ], str ] :
152150 def mount (constructor : ElementConstructor ) -> str :
153151 self ._views [view_id ] = constructor
154152 return view_id
@@ -161,5 +159,5 @@ def __call__(self, constructor: ElementConstructor) -> str:
161159 self ._views [view_id ] = constructor
162160 return view_id
163161
164- def __repr__ (self ):
162+ def __repr__ (self ) -> str :
165163 return f"{ type (self ).__name__ } ({ self ._views } )"
0 commit comments