Skip to content

fix(runtime-sdk): count entries in FormData.__len__ - #296

Merged
ryanking13 merged 1 commit into
cloudflare:mainfrom
0z5a:fix-formdata-len
Nov 23, 2026
Merged

ryanking13 merged 1 commit into
cloudflare:mainfrom
0z5a:fix-formdata-len

Conversation

@0z5a

@0z5a 0z5a commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #295.

FormData.__len__ called len() on the keys() generator, so len(form) and bool(form) raised TypeError: object of type 'generator' has no len(). It now counts what keys() yields, one entry per appended value, which matches the existing len(list(data.keys())) == 3 check.

How I found it

I was reading workers/formdata.py and saw that keys() is a generator while __len__ passes it to len().

Where this can show up in practice

Any handler that checks whether a submitted form is empty:

form = await request.form_data()
if not form:
    return Response("empty form", status=400)

bool() falls back to __len__, so the check raises and the request ends in a 500 instead of the 400.

Test Plan

  • Added len()/bool() asserts to test_form_data_unit_tests: a one-entry form, an empty FormData(), and the form with a repeated key (length 3). The existing keys()/items() asserts stay as the control.

  • I did not run the workerd suite locally. I checked the change on macOS with CPython 3.13 by loading workers/formdata.py with a small stand-in for the JS FormData object:

    before after
    len(list(form.keys())) (control) 3 3
    len(form) TypeError 3
    bool(form) TypeError True
    len(FormData()) TypeError 0
    bool(FormData()) TypeError False
  • ruff check and ruff format --check pass on both files.

FormData.__len__ called len() on the keys() generator, so len(form)
and bool(form) raised TypeError. Count the entries keys() yields
instead, one per appended value, which matches the existing keys()
test.

Refs cloudflare#295
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@clanky-the-clanker

Copy link
Copy Markdown

This pull request was not reviewed automatically because its author does not have write access to this repository. A maintainer with write access can start a review by commenting clanky review (or clanker review or @clanky-the-clanker review), optionally followed by instructions (e.g. clanky review focus on the retry logic).

@0z5a

0z5a commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Nov 21, 2026

@ryanking13 ryanking13 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks good to me overall. It is slightly annoying that len() is O(n), but I guess there will not be a common case where FormData has a lot of keys that time complexity matters.

@ryanking13
ryanking13 merged commit 6ff48b7 into cloudflare:main Nov 23, 2026
17 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

runtime-sdk: len() and bool() on FormData raise TypeError

2 participants