Skip to content

Preserve scalar leaves in nested form data - #410

Draft
lexingtonhibiki wants to merge 1 commit into
requests:mainfrom
lexingtonhibiki:fix/formdata-scalar-leaves-20261023
Draft

lexingtonhibiki wants to merge 1 commit into
requests:mainfrom
lexingtonhibiki:fix/formdata-scalar-leaves-20261023

Conversation

@lexingtonhibiki

@lexingtonhibiki lexingtonhibiki commented Oct 23, 2026 •

Copy link
Copy Markdown

Problem

formdata.urlencode accepts a scalar such as page=1 on its own, but adding a valid nested sibling makes the scalar fail during flattening:

from requests_toolbelt.utils.formdata import urlencode

urlencode([('page', 1)])  # 'page=1'
urlencode([('page', 1), ('filter', {'name': 'report'})])
# TypeError: 'int' object is not iterable

The expansion loop runs while any dict/list/tuple remains. Its helper then tries to unpack every non-string value, including non-iterable scalar leaves. This also occurs at deeper levels and with booleans, None, and floats.

Change

After the existing mapping-to-pairs conversion, leave values without either the iterator or sequence protocol for the standard-library encoder. Keep the existing strings/bytes path and expansion of mappings, generators, custom pair iterables, and legacy __getitem__ sequences. Exceptions from items(), iterator construction/consumption, and malformed pairs still propagate.

This preserves the encoder's existing scalar conversion rather than introducing an array encoding convention. It does not address #350 or #367, change doseq, or add dependencies.

Validation

Python 3.12, local focused form-data module with socket, DNS and subprocess guards:

  • Original source with the new regressions: 40 failed, 84 passed.
  • Fixed source: 124 passed.
  • Guard events: 0 in both runs.
  • Coverage includes dictionary/list inputs, flat/single-leaf/sibling/deep-sibling shapes, seven scalar values, both doseq values, and existing custom-container/error behavior.
  • Independent AI review also checked 22 compatibility cases and 12 scalar cases; all passed after narrowing the initial candidate to preserve nonbuiltin iterables.
  • git diff --check and Python syntax checks pass.

The guarded runner preloads Betamax before replacing socket entry points, producing one pytest assertion-rewrite warning. The full-suite check below is separate from this focused run. Full tox, older Python versions, local Sphinx, and flake8 have not been validated here; hosted Actions remains approval-gated. No human review is claimed.

Full-suite follow-up

Ran the complete suite on Linux/Python 3.12.14 in fresh archives of this exact head (d170830) and upstream base (bcd5f7b), using the same existing dependency environment and offline guard. No tests were deselected.

  • Strict DeprecationWarning-as-error: candidate 27 failed, 259 passed, 5 skipped; base 27 failed, 139 passed, 5 skipped. The 27 failing test IDs match exactly and fail on pre-existing Betamax/pyOpenSSL deprecations.
  • Keeping that strict policy but filtering only the three known message prefixes (datetime.datetime.utcnow(), Passing pyOpenSSL X509 objects is deprecated, and Passing pyOpenSSL PKey objects is deprecated): candidate 286 passed, 5 skipped; base 166 passed, 5 skipped.
  • All four runs report 0 guard events. The five skips are unchanged upstream skips: four host-header SSL tests and one X509 test.

This establishes no additional full-suite failures in this environment, not a strict-warning or supported-version matrix pass. Dependencies include requests 2.32.3, urllib3 2.8.0, Betamax 0.9.0 and pyOpenSSL 25.3.0.

Prepared and independently reviewed with AI assistance.

Keep non-iterable values for the standard URL encoder when a sibling
requires nested expansion. Preserve existing iterable pair containers
and propagate their errors instead of changing array conventions.
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.

1 participant