Skip to content

[rejected AI] Handle Mapping subclasses in urlencode filter - #2273

Closed
Rishini-Dharan wants to merge 1 commit into
pallets:mainfrom
Rishini-Dharan:urlencode-mapping-handling
Closed

Rishini-Dharan wants to merge 1 commit into
pallets:mainfrom
Rishini-Dharan:urlencode-mapping-handling

Conversation

@Rishini-Dharan

Copy link
Copy Markdown

What changed

The urlencode filter only treated concrete dict instances as key/value maps. Any other Mapping (such as collections.UserDict) fell through to the "iterable of pairs" branch, which iterates the mapping's keys and then unpacks each one as a (key, value) pair.

For a UserDict like {"Aa": "PersonA@123.com"}, the key "Aa" was unpacked into ("A", "a"), producing the garbage result A=a&B=b. For single-character keys the unpack raises ValueError: not enough values to unpack (expected 2, got 1). Plain dict worked, which made the behavior inconsistent with the documented contract ("A dict or iterable of (key, value) pairs").

The type check is changed from isinstance(value, dict) to isinstance(value, abc.Mapping), so mappings that are not concrete dict instances are handled through value.items() the same way as dict.

Why

Mapping subclasses are valid URL query sources and per the docstring should be treated as key/value pairs. This fixes both the mangled output and the crash, matching the behavior of plain dict.

Tests

Extended the parametrized test_urlencode case with:

  • UserDict({"f": 1}) - general non-dict mapping with a multi-char key.
  • The exact reported scenario: UserDict({"A": "PersonA@123.com", "B": "PersonB@123.com"}).

Both fail without the fix (mangled output / ValueError) and pass with it.

Validation run locally:

  • Full suite: 913 passed (pytest, filterwarnings=error).
  • ruff check and ruff format --check: clean on changed files.
  • mypy (1.15.0, the pinned CI version): success.

A CHANGES.rst entry was added under the unreleased 3.2.0 section.

fixes #2269

@davidism

Copy link
Copy Markdown
Member

@davidism davidism closed this Sep 11, 2026
@davidism davidism changed the title Handle Mapping subclasses in urlencode filter [rejected AI] Handle Mapping subclasses in urlencode filter Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Something wrong occurred when generating a string for UserDict

2 participants