Skip to content

Commit a9ccadb

Browse files
authored
Merge pull request #85 from volfpeter/feat/allow-Response-in-route-type-hints
Allow Response in route type hints
2 parents 7b87cb4 + e30275d commit a9ccadb

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010
- synchronize
1111

1212
jobs:
13-
Linters:
13+
Tests:
1414
runs-on: ubuntu-latest
1515
steps:
1616
- name: Checkout repository

fasthx/core_decorators.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def hx(
1616
*,
1717
no_data: bool = False,
1818
render_error: RenderFunction[Exception] | None = None,
19-
) -> Callable[[MaybeAsyncFunc[P, T]], Callable[P, Coroutine[None, None, T | Response]]]:
19+
) -> Callable[[MaybeAsyncFunc[P, T | Response]], Callable[P, Coroutine[None, None, T | Response]]]:
2020
"""
2121
Decorator that converts a FastAPI route's return value into HTML if the request was
2222
an HTMX one.
@@ -31,7 +31,9 @@ def hx(
3131
The rendered HTML for HTMX requests, otherwise the route's unchanged return value.
3232
"""
3333

34-
def decorator(func: MaybeAsyncFunc[P, T]) -> Callable[P, Coroutine[None, None, T | Response]]:
34+
def decorator(
35+
func: MaybeAsyncFunc[P, T | Response],
36+
) -> Callable[P, Coroutine[None, None, T | Response]]:
3537
@wraps(func)
3638
async def wrapper(
3739
__hx_request: DependsHXRequest, *args: P.args, **kwargs: P.kwargs
@@ -84,7 +86,7 @@ def page(
8486
render: RenderFunction[T],
8587
*,
8688
render_error: RenderFunction[Exception] | None = None,
87-
) -> Callable[[MaybeAsyncFunc[P, T]], Callable[P, Coroutine[None, None, Response]]]:
89+
) -> Callable[[MaybeAsyncFunc[P, T | Response]], Callable[P, Coroutine[None, None, Response]]]:
8890
"""
8991
Decorator that converts a FastAPI route's return value into HTML.
9092
@@ -94,7 +96,7 @@ def page(
9496
If not `None`, it is expected to raise an error if the exception can not be rendered.
9597
"""
9698

97-
def decorator(func: MaybeAsyncFunc[P, T]) -> Callable[P, Coroutine[None, None, Response]]:
99+
def decorator(func: MaybeAsyncFunc[P, T | Response]) -> Callable[P, Coroutine[None, None, Response]]:
98100
@wraps(func)
99101
async def wrapper(
100102
__page_request: DependsPageRequest, *args: P.args, **kwargs: P.kwargs

fasthx/htmy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def hx(
138138
*,
139139
error_component_selector: HTMYComponentSelector[Exception] | None = None,
140140
no_data: bool = False,
141-
) -> Callable[[MaybeAsyncFunc[P, T]], Callable[P, Coroutine[None, None, T | Response]]]:
141+
) -> Callable[[MaybeAsyncFunc[P, T | Response]], Callable[P, Coroutine[None, None, T | Response]]]:
142142
"""
143143
Decorator for rendering the route's result if the request was an HTMX one.
144144
@@ -163,7 +163,7 @@ def page(
163163
component_selector: HTMYComponentSelector[T] | None = None,
164164
*,
165165
error_component_selector: HTMYComponentSelector[Exception] | None = None,
166-
) -> Callable[[MaybeAsyncFunc[P, T]], Callable[P, Coroutine[None, None, T | Response]]]:
166+
) -> Callable[[MaybeAsyncFunc[P, T | Response]], Callable[P, Coroutine[None, None, T | Response]]]:
167167
"""
168168
Decorator for rendering a route's result.
169169

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fasthx"
3-
version = "3.0.0"
3+
version = "3.0.1"
44
description = "FastAPI server-side rendering with built-in HTMX support."
55
authors = ["Peter Volf <[email protected]>"]
66
readme = "README.md"

tests/test_htmy.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ def index() -> list[User]:
3838

3939
@app.get("/htmx-or-data")
4040
@htmy.hx(UserList)
41-
def htmx_or_data(response: Response) -> list[User]:
41+
def htmx_or_data(
42+
response: Response,
43+
) -> list[User]:
4244
response.headers["test-header"] = "exists"
4345
return users
4446

0 commit comments

Comments
 (0)