Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

### Features

* Allow None values through and skip them.
* Allow primitives through without having to cast them to str first.
* Add Decimal as an allowed primitive.

Expand Down
7 changes: 4 additions & 3 deletions hypermedia/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __init__(
**attributes: Any,
) -> None:
"""Initialize Root with children."""
self.children = children
self.children = tuple(c for c in children if c is not None)
self.slot = slot
self.slots = get_slots(self)
self.attributes = attributes
Expand All @@ -117,10 +117,11 @@ def extend(self, slot: str, *children: AnyChildren) -> Self:
raise ValueError(f"Could not find a slot with name: {slot}")
element = self.slots[slot]

element.children = element.children + children
new_children = tuple(child for child in children if child is not None)
element.children = element.children + new_children

get_child_slots(
self.slots, [c for c in children if isinstance(c, Element)]
self.slots, [c for c in new_children if isinstance(c, Element)]
)
return self

Expand Down
2 changes: 1 addition & 1 deletion hypermedia/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SafeString(str):
ComplexChildren: TypeAlias = "Element"
"""Type alias for elements that are allowed to have only non-primitive children.""" # noqa: E501

AnyChildren: TypeAlias = Union[PrimitiveChildren, ComplexChildren]
AnyChildren: TypeAlias = Union[PrimitiveChildren, ComplexChildren, None]
"""Type alias for elements that are allowed to have any children."""

# PLC0105 `TypeVar` name "TChildren" does not reflect its covariance;
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,10 @@ def test_void_elements_and_aliases(
) -> None:
assert element().dump() == result
assert alias().dump() == result


def test_none_children_are_skipped() -> None:
assert Bold(None).dump() == "<b></b>"
assert Bold("Test", None).dump() == "<b>Test</b>"
assert Bold(None, "Test").dump() == "<b>Test</b>"
assert Bold(None, None).dump() == "<b></b>"
17 changes: 17 additions & 0 deletions tests/models/element/test_extend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ def test_extend_adds_child_to_slot() -> None:
assert element.children == (child,)


def test_extend_skips_adding_none_child_to_slot() -> None:
element = TestElement(slot="my_slot")

element.extend("my_slot", None)

assert element.children == ()


def test_extend_skips_only_none_values() -> None:
element = TestElement(slot="my_slot")
child = TestElement()

element.extend("my_slot", child, None)

assert element.children == (child,)


def test_extend_adds_children_to_slot() -> None:
element = TestElement(slot="my_slot")
child_1 = TestElement()
Expand Down
1 change: 1 addition & 0 deletions tests/models/element/test_render_children.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def test_renders_safe_string_children_as_is() -> None:
def test_primitive_types_are_rendered() -> None:
assert TestElement(1)._render_children() == "1"
assert TestElement(1.1)._render_children() == "1.1"
assert TestElement(None)._render_children() == ""
assert TestElement(True)._render_children() == "True"
assert TestElement(False)._render_children() == "False"
assert TestElement("test")._render_children() == "test"
Expand Down
Loading