Skip to content

Commit

Permalink
handle for when there is a type hint on session
Browse files Browse the repository at this point in the history
  • Loading branch information
henriwoodcock committed Feb 7, 2025
1 parent 7737c91 commit d492594
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ async def _find_p(req, arg:str, p:Parameter):
if issubclass(anno, Request): return req
if issubclass(anno, HtmxHeaders): return _get_htmx(req.headers)
if issubclass(anno, Starlette): return req.scope['app']
if _is_body(anno) and 'session'.startswith(arg.lower()): return req.scope.get('session', {})
if _is_body(anno): return await _from_body(req, p)
# If there's no annotation, check for special names
if anno is empty:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_toaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def post(session):
def get(session):
return Titled("Hello, world!", P(str(session)))

@rt("/see-toast-with-typehint")
def get(session: dict):
return Titled("Hello, world!", P(str(session)))

@rt("/see-toast-ft-response")
def get(session):
add_toast(session, "Toast FtResponse", "info")
Expand All @@ -41,6 +45,13 @@ def test_ft_response():
res = cli.get('/see-toast-ft-response')
assert 'Toast FtResponse' in res.text

def test_get_toaster_with_typehint():
res = cli.get('/see-toast-with-typehint', follow_redirects=False)
assert 'Toast get' in res.text

res = cli.get('/see-toast-with-typehint', follow_redirects=True)
assert 'Toast get' in res.text

test_get_toaster()
test_post_toaster()
test_ft_response()
Expand Down

0 comments on commit d492594

Please sign in to comment.