Skip to content

Commit 2dc5d44

Browse files
committed
Fix @class_component signature
1 parent 33df314 commit 2dc5d44

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/reactpy_utils/class_component.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import annotations
22

33
import inspect
4-
from typing import TYPE_CHECKING, Any, TypeVar, cast
4+
from typing import TYPE_CHECKING, Any, TypeVar, cast, Callable
55

66
from reactpy.core.component import Component
7+
from reactpy.core.types import ComponentType
78

89
if TYPE_CHECKING:
910
from reactpy.core.types import VdomDict
@@ -53,6 +54,13 @@ def create_component(*args: Any, key: Any | None = None, **kwargs: Any):
5354
_comp = cast(_ComponentClass, _comp)
5455
_comp._sig = sig # pylint: disable=protected-access
5556
_comp.key = key
57+
58+
_comp._args = args
59+
_comp._kwargs = kwargs
60+
61+
_comp.type = cast(Callable[..., ComponentType],comp)
62+
63+
5664
return _comp
5765

5866
return create_component # type: ignore

tests/test_class_component.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import pytest
2+
from reactpy import html
3+
from reactpy.core.types import VdomDict
24
from reactpy.testing import DisplayFixture
35

46
from docs.examples.python.class_component import App
@@ -7,19 +9,37 @@
79
from .tooling import page_stable
810

911

10-
@class_component
11-
class RenderMissing:
12-
def __init__(self):
13-
super().__init__()
12+
def test_render_missing(display: DisplayFixture):
1413

14+
@class_component
15+
class RenderMissing:
16+
"""Class must have a render() method"""
17+
18+
def __init__(self):
19+
super().__init__()
1520

16-
def test_render_missing(display: DisplayFixture):
1721
try:
1822
component = RenderMissing()
1923
component.render() # type: ignore
2024
except NotImplementedError:
2125
assert True
2226

27+
def test_str(display: DisplayFixture):
28+
29+
@class_component
30+
class TitleComponent:
31+
32+
def __init__(self, title:str):
33+
super().__init__()
34+
self.title = title
35+
36+
def render(self) -> VdomDict:
37+
return html.h2(self.title)
38+
39+
component = TitleComponent(title="Main Page")
40+
assert f"{component}" == f"TitleComponent({id(component):02x}, title='Main Page')"
41+
42+
2343

2444
@pytest.mark.anyio
2545
async def test_component_class_new(display: DisplayFixture):

0 commit comments

Comments
 (0)