Skip to content

Commit 13cc71a

Browse files
committed
get clever with functions
1 parent 6450049 commit 13cc71a

File tree

7 files changed

+33
-43
lines changed

7 files changed

+33
-43
lines changed

src/idom/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from .core.component import component
55
from .core.events import event
66
from .core.hooks import (
7-
Context,
87
create_context,
98
use_callback,
109
use_context,
@@ -28,7 +27,6 @@
2827
__all__ = [
2928
"component",
3029
"config",
31-
"Context",
3230
"create_context",
3331
"event",
3432
"hooks",

src/idom/backend/flask.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@
3737

3838
logger = logging.getLogger(__name__)
3939

40-
ConnectionContext: Context[Connection | None] = create_context(
41-
None, "ConnectionContext"
42-
)
40+
ConnectionContext: Context[Connection | None] = create_context(None)
4341

4442

4543
def configure(

src/idom/backend/sanic.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232

3333
logger = logging.getLogger(__name__)
3434

35-
ConnectionContext: Context[Connection | None] = create_context(
36-
None, "ConnectionContext"
37-
)
35+
ConnectionContext: Context[Connection | None] = create_context(None)
3836

3937

4038
def configure(

src/idom/backend/starlette.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
logger = logging.getLogger(__name__)
3232

33-
WebSocketContext: Context[WebSocket | None] = create_context(None, "WebSocketContext")
33+
WebSocketContext: Context[WebSocket | None] = create_context(None)
3434

3535

3636
def configure(

src/idom/backend/tornado.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
from .utils import CLIENT_BUILD_DIR, safe_client_build_dir_path
2727

2828

29-
ConnectionContext: Context[Connection | None] = create_context(
30-
None, "ConnectionContext"
31-
)
29+
ConnectionContext: Context[Connection | None] = create_context(None)
3230

3331

3432
def configure(

src/idom/core/hooks.py

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
cast,
2121
overload,
2222
)
23-
from warnings import warn
2423

2524
from typing_extensions import Protocol
2625

2726
from idom.config import IDOM_DEBUG_MODE
2827
from idom.utils import Ref
2928

29+
from ._f_back import f_module_name
3030
from ._thread_local import ThreadLocal
3131
from .types import ComponentType, Key, VdomDict
3232
from .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

281274
def 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")

tests/test_core/test_hooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ def SomeComponent():
12491249
async def test_conditionally_rendered_components_can_use_context():
12501250
set_state = idom.Ref()
12511251
used_context_values = []
1252-
some_context = idom.Context("some_context", None)
1252+
some_context = idom.create_context("some_context", None)
12531253

12541254
@idom.component
12551255
def SomeComponent():

0 commit comments

Comments
 (0)