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
4
2
5
3
from idom .core import hooks
6
4
from idom .core .element import ElementConstructor , element
7
5
from idom .utils import Ref
8
6
9
7
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 ]
17
9
18
10
19
11
def hotswap (shared : bool = False ) -> Tuple [MountFunc , ElementConstructor ]:
@@ -82,16 +74,22 @@ def swap(constructor: Callable[[], Any]) -> None:
82
74
def HotSwap () -> Any :
83
75
return constructor_ref .current ()
84
76
85
- def swap (constructor : ElementConstructor ) -> None :
77
+ def swap (constructor : Callable [[], Any ] ) -> None :
86
78
constructor_ref .current = constructor
87
79
return None
88
80
89
- return cast (MountFunc , swap ), HotSwap
81
+ return swap , HotSwap
82
+
83
+
84
+ _Func = Callable [..., Any ]
85
+ _FuncVar = TypeVar ("_FuncVar" , bound = _Func )
90
86
91
87
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 )
95
93
96
94
97
95
def multiview () -> Tuple ["MultiViewMount" , ElementConstructor ]:
@@ -148,7 +146,7 @@ def __init__(self, views: Dict[str, ElementConstructor]):
148
146
def remove (self , view_id : str ) -> None :
149
147
del self ._views [view_id ]
150
148
151
- def __getitem__ (self , view_id : str ) -> MountFunc :
149
+ def __getitem__ (self , view_id : str ) -> Callable [[ ElementConstructor ], str ] :
152
150
def mount (constructor : ElementConstructor ) -> str :
153
151
self ._views [view_id ] = constructor
154
152
return view_id
@@ -161,5 +159,5 @@ def __call__(self, constructor: ElementConstructor) -> str:
161
159
self ._views [view_id ] = constructor
162
160
return view_id
163
161
164
- def __repr__ (self ):
162
+ def __repr__ (self ) -> str :
165
163
return f"{ type (self ).__name__ } ({ self ._views } )"
0 commit comments