Skip to content

Commit

Permalink
Merge pull request #31 from volfpeter/feat/formatter-skip-None
Browse files Browse the repository at this point in the history
Formatter: skip properties whose value is None
  • Loading branch information
volfpeter authored Jan 15, 2025
2 parents a9ad84c + dec0a2e commit dc9a2a4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
9 changes: 8 additions & 1 deletion htmy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
from .utils import join_components

if TYPE_CHECKING:
from typing_extensions import Self
from typing_extensions import Never, Self
else:
Never = Any
Self = Any

# -- Utility components
Expand Down Expand Up @@ -348,6 +349,11 @@ class SkipProperty(Exception):

...

@classmethod
def format_property(cls, _: Any) -> Never:
"""Property formatter that raises a `SkipProperty` error regardless of the received value."""
raise cls("skip-property")


class Text(str):
"""Marker class for differentiating text content from other strings."""
Expand Down Expand Up @@ -484,6 +490,7 @@ def _base_formatters(self) -> dict[type, Callable[[Any], str]]:
date: lambda d: cast(date, d).isoformat(),
datetime: lambda d: cast(datetime, d).isoformat(),
XBool: lambda v: cast(XBool, v).format(),
type(None): SkipProperty.format_property,
}


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "htmy"
version = "0.4.1"
version = "0.4.2"
description = "Async, pure-Python rendering engine."
authors = ["Peter Volf <[email protected]>"]
license = "MIT"
Expand Down
3 changes: 2 additions & 1 deletion tests/test_main_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ async def async_fc(props: int, context: Context) -> Component:
),
dp_1=123,
_class="w-full",
none=None,
)
),
ErrorBoundary(
Expand Down Expand Up @@ -116,7 +117,7 @@ def rendered() -> str:
"sync_fc-int:987321",
"async_fc-int:456",
"<a_main >",
'<div dp-1="123" class="w-full">',
'<div dp-1="123" class="w-full" >',
"sd&lt;fs&gt; df",
'<h1 created-at="2024-10-03T04:42:02.000071+00:00" on_day="2024-10-03">sdfds</h1>',
"</div>",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
_blog_post = _blog_post_format_string.format(title="Essential reading")

_parsed_blog_post = """<h1>Essential reading</h1>
<div class="codehilite"><pre><span></span><code><span class="kn">import</span> <span class="nn">this</span>
<div class="codehilite"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">this</span>
</code></pre></div>
<p>Also available <a href="https://peps.python.org/pep-0020/">here</a>.</p>
Expand Down Expand Up @@ -97,7 +97,7 @@ def rules(cls) -> dict[str, Callable[..., ComponentType]]:


_base_etree_converted_blogpost = """<h1 {h1_attrs}>Essential reading</h1>
<div class="codehilite"><pre ><span ></span><code ><span class="kn">import</span> <span class="nn">this</span>
<div class="codehilite"><pre ><span ></span><code ><span class="kn">import</span><span class="w"> </span><span class="nn">this</span>
</code></pre></div>
<p >Also available <a href="https://peps.python.org/pep-0020/">here</a>.</p>
Expand Down

0 comments on commit dc9a2a4

Please sign in to comment.