File tree Expand file tree Collapse file tree 1 file changed +9
-7
lines changed Expand file tree Collapse file tree 1 file changed +9
-7
lines changed Original file line number Diff line number Diff line change 2626from idom .config import IDOM_DEBUG_MODE
2727from idom .utils import Ref
2828
29- from ._f_back import f_module_name
3029from ._thread_local import ThreadLocal
3130from .types import ComponentType , Key , VdomDict
3231from .vdom import vdom
@@ -280,12 +279,15 @@ def use_context(context: Context[_StateType]) -> _StateType:
280279 provider = hook .get_context_provider (context )
281280
282281 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+
289291 subscribers = provider ._subscribers
290292
291293 @use_effect
You can’t perform that action at this time.
0 commit comments