Repository navigation
fix(runtime-sdk): count entries in FormData.__len__ - #296
Merged
Merged
Conversation
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
Contributor
|
All contributors have signed the CLA ✍️ ✅ |
|
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 |
Contributor
Author
|
I have read the CLA Document and I hereby sign the CLA |
ryanking13
approved these changes
Nov 23, 2026
ryanking13
left a comment
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #295.
FormData.__len__calledlen()on thekeys()generator, solen(form)andbool(form)raisedTypeError: object of type 'generator' has no len(). It now counts whatkeys()yields, one entry per appended value, which matches the existinglen(list(data.keys())) == 3check.How I found it
I was reading
workers/formdata.pyand saw thatkeys()is a generator while__len__passes it tolen().Where this can show up in practice
Any handler that checks whether a submitted form is empty:
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 totest_form_data_unit_tests: a one-entry form, an emptyFormData(), and the form with a repeated key (length 3). The existingkeys()/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.pywith a small stand-in for the JSFormDataobject:len(list(form.keys()))(control)len(form)TypeErrorbool(form)TypeErrorTruelen(FormData())TypeErrorbool(FormData())TypeErrorFalseruff checkandruff format --checkpass on both files.