@@ -246,13 +246,13 @@ def use_context(context: Context[_Type]) -> _Type:
246
246
provider = hook .get_context_provider (context )
247
247
248
248
if provider is None :
249
- # force type checker to realize this is just a normal function
250
- assert isinstance (context , FunctionType ), f" { context } is not a Context"
251
- # __kwdefault__ can be None if no kwarg only parameters exist
252
- assert context .__kwdefaults__ is not None , f" { context } has no 'value' kwarg"
253
- # lastly check that 'value' kwarg exists
254
- assert "value" in context .__kwdefaults__ , f" { context } has no 'value' kwarg"
255
- # then we can safely access the context's default value
249
+ # same assertions but with normal exceptions
250
+ if not isinstance (context , FunctionType ):
251
+ raise TypeError ( f" { context } is not a Context" )
252
+ if context .__kwdefaults__ is None :
253
+ raise TypeError ( f" { context } has no 'value' kwarg" )
254
+ if "value" not in context .__kwdefaults__ :
255
+ raise TypeError ( f" { context } has no ' value' kwarg" )
256
256
return cast (_Type , context .__kwdefaults__ ["value" ])
257
257
258
258
return provider ._value
@@ -455,7 +455,7 @@ class _Memo(Generic[_Type]):
455
455
456
456
def empty (self ) -> bool :
457
457
try :
458
- self .value
458
+ self .value # noqa: B018
459
459
except AttributeError :
460
460
return True
461
461
else :
@@ -708,8 +708,8 @@ def set_current(self) -> None:
708
708
709
709
def unset_current (self ) -> None :
710
710
"""Unset this hook as the active hook in this thread"""
711
- # this assertion should never fail - primarilly useful for debug
712
- assert _hook_stack . get (). pop () is self
711
+ if _hook_stack . get (). pop () is not self :
712
+ raise RuntimeError ( "Hook stack is in an invalid state" )
713
713
714
714
def _schedule_render (self ) -> None :
715
715
try :
0 commit comments