Skip to content

Commit cc9ee42

Browse files
committed
require a my_app.idom.components attribute
1 parent f008f6b commit cc9ee42

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

src/django_idom/app_components.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,26 @@ def has_component(name: str) -> bool:
2727
logger.debug(f"Skipping {idom_mod_name!r} - does not exist")
2828
continue
2929

30-
if not hasattr(idom_mod, "__all__"):
30+
if not hasattr(idom_mod, "components"):
3131
logger.warning(
3232
f"'django_idom' expected module {idom_mod_name!r} to have an "
33-
"'__all__' attribute that lists its publically available components."
33+
"'components' attribute that lists its publically available components."
3434
)
3535
continue
3636

37-
for component_name in idom_mod.__all__:
38-
try:
39-
component_constructor = getattr(idom_mod, component_name)
40-
except AttributeError:
37+
for component_constructor in idom_mod.components:
38+
if not callable(component_constructor):
4139
logger.warning(
42-
f"Module {idom_mod_name!r} has no attribute {component_name!r}"
40+
f"'{idom_mod_name}.{component_name}' is not a callable component constructor"
4341
)
4442
continue
4543

46-
if not callable(component_constructor):
47-
logger.warning(f"'{idom_mod_name}.{component_name}' is not a component")
44+
try:
45+
component_name = getattr(component_constructor, "__name__")
46+
except AttributeError:
47+
logger.warning(
48+
f"Component constructor {component_constructor} has not attribute '__name__'"
49+
)
4850
continue
4951

5052
_LOADED_COMPONENTS[f"{app_mod_name}.{component_name}"] = component_constructor

tests/test_app/components.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import idom
2+
3+
4+
@idom.component
5+
def HelloWorld():
6+
return idom.html.h1({"id": "hello-world"}, "Hello World!")
7+
8+
9+
@idom.component
10+
def Button():
11+
count, set_count = idom.hooks.use_state(0)
12+
return idom.html.div(
13+
idom.html.button(
14+
{"id": "counter-inc", "onClick": lambda event: set_count(count + 1)},
15+
"Click me!",
16+
),
17+
idom.html.p(
18+
{"id": "counter-num", "data-count": count},
19+
f"Current count is: {count}",
20+
),
21+
)

tests/test_app/idom.py

+5-22
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
1-
import idom
1+
from .components import Button, HelloWorld
22

33

4-
__all__ = "HelloWorld", "Button"
5-
6-
7-
@idom.component
8-
def HelloWorld():
9-
return idom.html.h1({"id": "hello-world"}, "Hello World!")
10-
11-
12-
@idom.component
13-
def Button():
14-
count, set_count = idom.hooks.use_state(0)
15-
return idom.html.div(
16-
idom.html.button(
17-
{"id": "counter-inc", "onClick": lambda event: set_count(count + 1)},
18-
"Click me!",
19-
),
20-
idom.html.p(
21-
{"id": "counter-num", "data-count": count},
22-
f"Current count is: {count}",
23-
),
24-
)
4+
components = [
5+
HelloWorld,
6+
Button,
7+
]

0 commit comments

Comments
 (0)