File tree Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Original file line number Diff line number Diff line change @@ -280,12 +280,15 @@ def use_context(context: Context[_StateType]) -> _StateType:
280280 provider = hook .get_context_provider (context )
281281
282282 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" )
283+ # force type checker to realize this is just a normal function
284+ assert isinstance (context , FunctionType ), f"{ context } is not a Context"
285+ # __kwdefault__ can be None if no kwarg only parameters exist
286+ assert context .__kwdefaults__ is not None , f"{ context } has no 'value' kwarg"
287+ # lastly check that 'value' kwarg exists
288+ assert "value" in context .__kwdefaults__ , f"{ context } has no 'value' kwarg"
289+ # then we can safely access the context's default value
290+ return cast (_StateType , context .__kwdefaults__ ["value" ])
291+
289292 subscribers = provider ._subscribers
290293
291294 @use_effect
You can’t perform that action at this time.
0 commit comments