Skip to content

Commit 5c0abf3

Browse files
committed
better vdom checks
1 parent 29c0e85 commit 5c0abf3

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

src/reactpy/core/vdom.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def separate_attributes_and_children(
199199

200200
_attributes: VdomAttributes
201201
children_or_iterables: Sequence[Any]
202-
if _is_attributes(values[0]):
202+
if type(values[0]) is dict:
203203
_attributes, *children_or_iterables = values
204204
else:
205205
_attributes = {}
@@ -246,10 +246,6 @@ def _flatten_children(children: Sequence[Any]) -> list[Any]:
246246
return _children
247247

248248

249-
def _is_attributes(value: Any) -> bool:
250-
return isinstance(value, Mapping) and "tagName" not in value
251-
252-
253249
def _is_single_child(value: Any) -> bool:
254250
if isinstance(value, (str, Mapping)) or not hasattr(value, "__iter__"):
255251
return True

src/reactpy/types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,9 @@ class VdomDict(dict):
796796
"""A dictionary representing a virtual DOM element."""
797797

798798
def __init__(self, **kwargs: Unpack[VdomTypeDict]) -> None:
799+
if "tagName" not in kwargs:
800+
msg = "VdomDict requires a 'tagName' key."
801+
raise ValueError(msg)
799802
invalid_keys = set(kwargs) - ALLOWED_VDOM_KEYS
800803
if invalid_keys:
801804
msg = f"Invalid keys: {invalid_keys}."

tests/test_core/test_vdom.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
(False, {}),
2020
(False, {"tagName": None}),
2121
(False, {"tagName": ""}),
22-
(False, VdomTypeDict()),
23-
(False, VdomDict()),
22+
(False, VdomTypeDict(tagName="div")),
2423
(True, VdomDict(tagName="")),
2524
(True, VdomDict(tagName="div")),
2625
],
@@ -71,7 +70,7 @@ def test_is_vdom(result, value):
7170
),
7271
(
7372
reactpy.Vdom(tagName="div")({"tagName": "div"}),
74-
{"tagName": "div", "children": [{"tagName": "div"}]},
73+
{"tagName": "div", "attributes": {"tagName": "div"}},
7574
),
7675
(
7776
reactpy.Vdom(tagName="div")((i for i in range(3))),
@@ -336,7 +335,10 @@ def test_invalid_vdom_keys():
336335
reactpy.Vdom()
337336

338337
with pytest.raises(ValueError, match="Invalid keys:*"):
339-
reactpy.types.VdomDict(foo="bar")
338+
reactpy.types.VdomDict(tagName="test", foo="bar")
340339

341340
with pytest.raises(KeyError, match="Invalid key:*"):
342-
reactpy.types.VdomDict()["foo"] = "bar"
341+
reactpy.types.VdomDict(tagName="test")["foo"] = "bar"
342+
343+
with pytest.raises(ValueError, match="VdomDict requires a 'tagName' key."):
344+
reactpy.types.VdomDict(foo="bar")

0 commit comments

Comments
 (0)