File tree 1 file changed +9
-7
lines changed
1 file changed +9
-7
lines changed Original file line number Diff line number Diff line change 26
26
from idom .config import IDOM_DEBUG_MODE
27
27
from idom .utils import Ref
28
28
29
- from ._f_back import f_module_name
30
29
from ._thread_local import ThreadLocal
31
30
from .types import ComponentType , Key , VdomDict
32
31
from .vdom import vdom
@@ -280,12 +279,15 @@ def use_context(context: Context[_StateType]) -> _StateType:
280
279
provider = hook .get_context_provider (context )
281
280
282
281
if provider is None :
283
- try :
284
- # force type checker to realize this is just a normal function
285
- assert isinstance (context , FunctionType )
286
- return cast (_StateType , context .__kwdefaults__ ["value" ])
287
- except KeyError :
288
- raise TypeError (f"{ context } does not implement the Context interface" )
282
+ # force type checker to realize this is just a normal function
283
+ assert isinstance (context , FunctionType ), f"{ context } is not a Context"
284
+ # __kwdefault__ can be None if no kwarg only parameters exist
285
+ assert context .__kwdefaults__ is not None , f"{ context } has no 'value' kwarg"
286
+ # lastly check that 'value' kwarg exists
287
+ assert "value" in context .__kwdefaults__ , f"{ context } has no 'value' kwarg"
288
+ # then we can safely access the context's default value
289
+ return cast (_StateType , context .__kwdefaults__ ["value" ])
290
+
289
291
subscribers = provider ._subscribers
290
292
291
293
@use_effect
You can’t perform that action at this time.
0 commit comments