Skip to content

Commit 743b955

Browse files
committed
Make ContextProvider inherit from Component
1 parent 11e6a9a commit 743b955

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

src/reactpy/core/layout.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
)
1313
from collections import Counter
1414
from collections.abc import Callable, Sequence
15-
from contextlib import AsyncExitStack
15+
from contextlib import AsyncExitStack, suppress
1616
from logging import getLogger
1717
from types import TracebackType
1818
from typing import (
@@ -73,7 +73,7 @@ class Layout:
7373
def __init__(self, root: Component | Context[Any] | ContextProvider[Any]) -> None:
7474
super().__init__()
7575
if not isinstance(root, Component):
76-
msg = f"Expected a ComponentType, not {type(root)!r}."
76+
msg = f"Expected a ReactPy component, not {type(root)!r}."
7777
raise TypeError(msg)
7878
self.root = root
7979

@@ -98,11 +98,8 @@ async def __aexit__(
9898

9999
for t in self._render_tasks:
100100
t.cancel()
101-
try:
101+
with suppress(CancelledError):
102102
await t
103-
except CancelledError:
104-
pass
105-
106103
await self._unmount_model_states([root_model_state])
107104

108105
# delete attributes here to avoid access after exiting context manager

src/reactpy/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ def __call__(
10131013
) -> ContextProvider[_Type]: ...
10141014

10151015

1016-
class ContextProvider(Generic[_Type]):
1016+
class ContextProvider(Component, Generic[_Type]):
10171017
def __init__(
10181018
self,
10191019
*children: Any,

tests/test_core/test_layout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ def MyComponent(): ...
5656

5757

5858
def test_layout_expects_abstract_component():
59-
with pytest.raises(TypeError, match="Expected a ComponentType"):
59+
with pytest.raises(TypeError, match="Expected a ReactPy component"):
6060
Layout(None)
61-
with pytest.raises(TypeError, match="Expected a ComponentType"):
61+
with pytest.raises(TypeError, match="Expected a ReactPy component"):
6262
Layout(reactpy.html.div())
6363

6464

tests/test_web/test_module.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,12 @@ def App():
422422

423423
def test_reactjs_component_from_string():
424424
reactpy.web.reactjs_component_from_string(
425-
"temp", "old", "Component", resolve_imports=False
425+
"old", "Component", resolve_imports=False, name="temp"
426426
)
427427
reactpy.web.module._STRING_WEB_MODULE_CACHE.clear()
428428
with assert_reactpy_did_log(r"Existing web module .* will be replaced with"):
429429
reactpy.web.reactjs_component_from_string(
430-
"temp", "new", "Component", resolve_imports=False
430+
"new", "Component", resolve_imports=False, name="temp"
431431
)
432432

433433

@@ -452,7 +452,7 @@ def ShowSimpleButton():
452452

453453
async def test_reactjs_component_from_file(display: DisplayFixture):
454454
SimpleButton = reactpy.web.reactjs_component_from_file(
455-
"simple-button", JS_FIXTURES_DIR / "simple-button.js", "SimpleButton"
455+
JS_FIXTURES_DIR / "simple-button.js", "SimpleButton", name="simple-button"
456456
)
457457

458458
is_clicked = reactpy.Ref(False)
@@ -493,13 +493,13 @@ def test_reactjs_component_from_file_caching(tmp_path):
493493
name = "test-file-module"
494494
reactpy.web.module._FILE_WEB_MODULE_CACHE.clear()
495495

496-
reactpy.web.reactjs_component_from_file(name, file, "Component")
496+
reactpy.web.reactjs_component_from_file(file, "Component", name=name)
497497
key = next(x for x in reactpy.web.module._FILE_WEB_MODULE_CACHE.keys() if name in x)
498498
module1 = reactpy.web.module._FILE_WEB_MODULE_CACHE[key]
499499
assert module1
500500
initial_length = len(reactpy.web.module._FILE_WEB_MODULE_CACHE)
501501

502-
reactpy.web.reactjs_component_from_file(name, file, "Component")
502+
reactpy.web.reactjs_component_from_file(file, "Component", name=name)
503503
assert len(reactpy.web.module._FILE_WEB_MODULE_CACHE) == initial_length
504504

505505

@@ -508,13 +508,24 @@ def test_reactjs_component_from_string_caching():
508508
content = "export function Component() {}"
509509
reactpy.web.module._STRING_WEB_MODULE_CACHE.clear()
510510

511-
reactpy.web.reactjs_component_from_string(name, content, "Component")
511+
reactpy.web.reactjs_component_from_string(content, "Component", name=name)
512512
key = next(
513513
x for x in reactpy.web.module._STRING_WEB_MODULE_CACHE.keys() if name in x
514514
)
515515
module1 = reactpy.web.module._STRING_WEB_MODULE_CACHE[key]
516516
assert module1
517517
initial_length = len(reactpy.web.module._STRING_WEB_MODULE_CACHE)
518518

519-
reactpy.web.reactjs_component_from_string(name, content, "Component")
519+
reactpy.web.reactjs_component_from_string(content, "Component", name=name)
520+
assert len(reactpy.web.module._STRING_WEB_MODULE_CACHE) == initial_length
521+
522+
523+
def test_reactjs_component_from_string_with_no_name():
524+
content = "export function Component() {}"
525+
reactpy.web.module._STRING_WEB_MODULE_CACHE.clear()
526+
527+
reactpy.web.reactjs_component_from_string(content, "Component")
528+
initial_length = len(reactpy.web.module._STRING_WEB_MODULE_CACHE)
529+
530+
reactpy.web.reactjs_component_from_string(content, "Component")
520531
assert len(reactpy.web.module._STRING_WEB_MODULE_CACHE) == initial_length

0 commit comments

Comments
 (0)