fasthtml icon indicating copy to clipboard operation
fasthtml copied to clipboard

[BUG] "_handle got multiple values for argument f" when a POST route takes `f` as an argument

Open ContrapunctusII opened this issue 9 months ago • 2 comments

Describe the bug When a POST route take f as an argument for the form data, the argument name is in conflict with the argument f defined in _handle (core.py) :

async def _handle(f, *args, **kwargs):
    return (await f(*args, **kwargs)) if is_async_callable(f) else await run_in_threadpool(f, *args, **kwargs)
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/uvicorn/protocols/http/httptools_impl.py", line 416, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        self.scope, self.receive, self.send
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/starlette/applications.py", line 107, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/starlette/middleware/sessions.py", line 85, in __call__
    await self.app(scope, receive, send_wrapper)
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/starlette/middleware/exceptions.py", line 63, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/starlette/routing.py", line 716, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/starlette/routing.py", line 736, in app
    await route.handle(scope, receive, send)
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/starlette/routing.py", line 290, in handle
    await self.app(scope, receive, send)
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/starlette/routing.py", line 78, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
  File "/home/a/Code/a/.venv/lib/python3.14/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
  File "/home/a/Code/a/.venv/lib/python3.14/site-packages/starlette/routing.py", line 75, in app
    response = await f(request)
               ^^^^^^^^^^^^^^^^
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/fasthtml/core.py", line 629, in _f
    resp = await _wrap_call(f, req, sig.parameters)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/x/Code/a/.venv/lib/python3.14/site-packages/fasthtml/core.py", line 478, in _wrap_call
    return await _handle(f, **wreq)
                 ~~~~~~~^^^^^^^^^^^
TypeError: _handle() got multiple values for argument 'f'

(The **kwargs field will contain the name of the form, f in this case, which is already an argument of _handle.)

Minimal Reproducible Example

from fasthtml.common import *
from dataclasses import dataclass
import traceback

@dataclass
class DataForm:
    input: str
    def __init__(self, input: str = ""): store_attr()

app = FastHTML(hdrs=(picolink))

@app.route("/")
def get():
    return Div(Form(
        Input(name="input"),
        Button("Submit", _id="submit", type="submit", active="true"),
                hx_post="/post", hx_target="#result")), Div(P(_id="result"))
    
@app.route('/post')
def post(f: DataForm):
    try:
        return Response(f"<p>{f.input}</p")
    except Exception as e:
        traceback.print_exc()
    
if __name__ == "__main__":
    serve()

Expected behavior No error, input written in the webpage.

Environment Information Please provide the following version information:

  • fastlite version: 0.2.4
  • fastcore version: 1.12.16
  • fasthtml version: 0.12.47

Confirmation Please confirm the following:

  • [x] I have read the FAQ (https://docs.fastht.ml/explains/faq.html)
  • [x] I have provided a minimal reproducible example
  • [x] I have included the versions of fastlite, fastcore, and fasthtml
  • [x] I understand that this is a volunteer open source project with no commercial support.

Additional context This issue is a bit infortunate since it happens only because of the argument name chosen by the user for his POST route. It required me some time to figure out what was going on.

An easy workaround would be to throw an exception when the argument f is used so as to warn the user.

ContrapunctusII avatar Feb 22 '26 10:02 ContrapunctusII

Submitted a fix in #840.

Root cause: _handle(f, **wreq) where f is the handler and wreq contains f from form data — Python raises "got multiple values for argument f".

Fix: make f positional-only by adding / after it in the signature. This is the standard Python pattern used by functools.partial, functools.reduce, etc.

AI-assisted testing, AI-assisted review

weiguangli-io avatar Mar 03 '26 01:03 weiguangli-io

Hi — I'd like to take a crack at this.

The root cause is that _handle(f, *args, **kwargs) uses f as a regular parameter name, so when form data includes a field named f, it arrives via **kwargs and collides with the positional f (the handler function itself). The fix is to make f positional-only: async def _handle(f, /, *args, **kwargs) — the standard Python pattern used by functools.partial, functools.reduce, etc.

I noticed PR #840 attempted this same fix but was withdrawn. I'll submit a clean PR with the notebook edit, an exported .py sync, and a regression test.

CrepuscularIRIS avatar Apr 15 '26 22:04 CrepuscularIRIS