Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle for when there is a type hint on session #651

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@
" if issubclass(anno, Request): return req\n",
" if issubclass(anno, HtmxHeaders): return _get_htmx(req.headers)\n",
" if issubclass(anno, Starlette): return req.scope['app']\n",
" if _is_body(anno) and 'session'.startswith(arg.lower()): return req.scope.get('session', {})\n",
" if _is_body(anno): return await _from_body(req, p)\n",
" # If there's no annotation, check for special names\n",
" if anno is empty:\n",
Expand Down Expand Up @@ -1652,7 +1653,7 @@
"source": [
"@app.get('/foo/{a}')\n",
"def foo(a:str, b:list[int]): ...\n",
" \n",
"\n",
"foo.to(a='bar', b=[1,2])"
]
},
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