2020 cast ,
2121 overload ,
2222)
23- from warnings import warn
2423
2524from typing_extensions import Protocol
2625
2726from idom .config import IDOM_DEBUG_MODE
2827from idom .utils import Ref
2928
29+ from ._f_back import f_module_name
3030from ._thread_local import ThreadLocal
3131from .types import ComponentType , Key , VdomDict
3232from .vdom import vdom
@@ -239,43 +239,36 @@ def use_debug_value(
239239 logger .debug (f"{ current_hook ().component } { new } " )
240240
241241
242- def create_context (
243- default_value : _StateType , name : str = "Context"
244- ) -> Context [_StateType ]:
242+ def create_context (default_value : _StateType ) -> Context [_StateType ]:
245243 """Return a new context type for use in :func:`use_context`"""
246- warn (
247- "The 'create_context' function is deprecated. "
248- "Use the 'Context' class instead. "
249- "This function will be removed in a future release."
250- )
251- return Context (name , default_value )
252244
245+ def context (
246+ * children : Any ,
247+ value : _StateType = default_value ,
248+ key : Key | None = None ,
249+ ) -> ContextProvider [_StateType ]:
250+ return ContextProvider (
251+ * children ,
252+ value = value ,
253+ key = key ,
254+ type = context ,
255+ )
253256
254- _UNDEFINED = cast ( Any , object ())
257+ context . __qualname__ = "context"
255258
259+ return context
256260
257- class Context (Generic [_StateType ]):
258- """Returns a :class:`ContextProvider` component"""
259261
260- def __init__ (self , name : str , default_value : _StateType ) -> None :
261- self .name = name
262- self .default_value = default_value
262+ class Context (Protocol [_StateType ]):
263+ """Returns a :class:`ContextProvider` component"""
263264
264265 def __call__ (
265266 self ,
266267 * children : Any ,
267- value : _StateType = _UNDEFINED ,
268- key : Key | None = None ,
268+ value : _StateType = ... ,
269+ key : Key | None = ... ,
269270 ) -> ContextProvider [_StateType ]:
270- return ContextProvider (
271- * children ,
272- value = self .default_value if value is _UNDEFINED else value ,
273- key = key ,
274- type = self ,
275- )
276-
277- def __repr__ (self ) -> str :
278- return f"{ type (self ).__name__ } ({ self .name !r} )"
271+ ...
279272
280273
281274def use_context (context : Context [_StateType ]) -> _StateType :
@@ -284,10 +277,15 @@ def use_context(context: Context[_StateType]) -> _StateType:
284277 See the full :ref:`Use Context` docs for more information.
285278 """
286279 hook = current_hook ()
287-
288280 provider = hook .get_context_provider (context )
281+
289282 if provider is None :
290- return context .default_value
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" )
291289 subscribers = provider ._subscribers
292290
293291 @use_effect
@@ -325,7 +323,7 @@ def should_render(self, new: ContextProvider[_StateType]) -> bool:
325323 return False
326324
327325 def __repr__ (self ) -> str :
328- return f"{ type (self ).__name__ } ({ self .type . name !r} )"
326+ return f"{ type (self ).__name__ } ({ self .type !r} )"
329327
330328
331329_ActionType = TypeVar ("_ActionType" )
0 commit comments