Skip to content

Commit 0f05a43

Browse files
authored
Merge pull request #20 from volfpeter/feat/metadata-always-exists
Always have Metadata in the htmy context
2 parents bfeecb2 + c5e5670 commit 0f05a43

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

holm/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.3.1"
1+
__version__ = "0.3.2"
22

33
from .app import App as App
44
from .fastapi import FastAPIDependency as FastAPIDependency

holm/modules/_metadata.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ class Metadata(ContextAware):
5050

5151
__slots__ = ("_metadata",)
5252

53-
def __init__(self, metadata: MetadataMapping) -> None:
53+
def __init__(self, metadata: MetadataMapping | None = None) -> None:
5454
"""
5555
Initialization.
5656
5757
Arguments:
5858
metadata: The actual metadata mapping.
5959
"""
60-
self._metadata = metadata
60+
self._metadata = {} if metadata is None else metadata
6161
"""The metadata mapping."""
6262

6363
@overload
@@ -116,8 +116,4 @@ def components_with_metadata(data: tuple[Component, MetadataMapping | None]) ->
116116
# This allows `Metadata.from_context()` to be used in all wrapped components.
117117
# The received components object is usually a ComponentSequence (doctype and html components),
118118
# so converting to a component sequence seems a tiny bit more efficient.
119-
return (
120-
components
121-
if metadata is None
122-
else Metadata(metadata).in_context(*as_component_sequence(components))
123-
)
119+
return Metadata(metadata).in_context(*as_component_sequence(components))

test_app/no_metadata/__init__.py

Whitespace-only changes.

test_app/no_metadata/page.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def page() -> str:
2+
return "This page has no metadata."
3+
4+
5+
rendered_page = """
6+
<!DOCTYPE html><html >
7+
<head >
8+
<title >App</title>
9+
<meta charset="utf-8"/>
10+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
11+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"/>
12+
</head>
13+
<body class="container-fluid">
14+
<header class="container">
15+
<p >Root layout header</p>
16+
</header>
17+
<main class="container">
18+
This page has no metadata.
19+
</main>
20+
<footer class="container">
21+
Root layout footer
22+
</footer>
23+
</body>
24+
</html>
25+
""".strip()

tests/no_metadata/__init__.py

Whitespace-only changes.

tests/no_metadata/test_page.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from fastapi.testclient import TestClient
2+
3+
from test_app.no_metadata.page import rendered_page
4+
5+
# Do not add metadata, Metadata.from_context() should still succeed in layouts.
6+
# metadata = {}
7+
8+
9+
def test_page(client: TestClient) -> None:
10+
response = client.get("/no_metadata")
11+
assert response.status_code == 200
12+
assert response.headers["Content-Type"] == "text/html; charset=utf-8"
13+
assert response.text == rendered_page

0 commit comments

Comments
 (0)