Skip to content

Commit

Permalink
Avoid including children in repr().
Browse files Browse the repository at this point in the history
It is tricky to get it right with lazy children. With non-lazy children,
the output can potentially be huge.
  • Loading branch information
pelme committed Sep 15, 2024
1 parent f27fc9c commit 08f0b45
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions htpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,6 @@ def _iter_context(self, ctx: dict[Context[t.Any], t.Any]) -> Iterator[str]:
yield from _iter_node_context(self._children, ctx)
yield f"</{self._name}>"

def __repr__(self) -> str:
return f"<{self.__class__.__name__} '{self}'>"

# Allow starlette Response.render to directly render this element without
# explicitly casting to str:
# https://github.com/encode/starlette/blob/5ed55c441126687106109a3f5e051176f88cd3e6/starlette/responses.py#L44-L49
Expand Down Expand Up @@ -308,6 +305,9 @@ def __getitem__(self: ElementSelf, children: Node) -> ElementSelf:
_validate_children(children)
return self.__class__(self._name, self._attrs, children) # pyright: ignore [reportUnknownArgumentType]

def __repr__(self) -> str:
return f"<{self.__class__.__name__} '<{self._name}{self._attrs}>...</{self._name}>'>"


class HTMLElement(Element):
def _iter_context(self, ctx: dict[Context[t.Any], t.Any]) -> Iterator[str]:
Expand All @@ -319,6 +319,9 @@ class VoidElement(BaseElement):
def _iter_context(self, ctx: dict[Context[t.Any], t.Any]) -> Iterator[str]:
yield f"<{self._name}{self._attrs}>"

def __repr__(self) -> str:
return f"<{self.__class__.__name__} '<{self._name}{self._attrs}>'>"


def render_node(node: Node) -> _Markup:
return _Markup("".join(iter_node(node)))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_invalid_element_name() -> None:


def test_element_repr() -> None:
assert repr(htpy.div("#a")) == """<Element '<div id="a"></div>'>"""
assert repr(htpy.div("#a")) == """<Element '<div id="a">...</div>'>"""


def test_void_element_repr() -> None:
Expand Down

0 comments on commit 08f0b45

Please sign in to comment.