Skip to content

Commit 9077af6

Browse files
committed
get coverage
1 parent 200fec2 commit 9077af6

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/idom/core/hooks.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)