Repository navigation
[rejected AI] Handle Mapping subclasses in urlencode filter - #2273
Closed
Rishini-Dharan wants to merge 1 commit into
Closed
Rishini-Dharan wants to merge 1 commit into
Rishini-Dharan wants to merge 1 commit into
Conversation
Member
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.
What changed
The
urlencodefilter only treated concretedictinstances as key/value maps. Any otherMapping(such ascollections.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
UserDictlike{"Aa": "PersonA@123.com"}, the key"Aa"was unpacked into("A", "a"), producing the garbage resultA=a&B=b. For single-character keys the unpack raisesValueError: not enough values to unpack (expected 2, got 1). Plaindictworked, 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)toisinstance(value, abc.Mapping), so mappings that are not concretedictinstances are handled throughvalue.items()the same way asdict.Why
Mappingsubclasses 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 plaindict.Tests
Extended the parametrized
test_urlencodecase with:UserDict({"f": 1})- general non-dictmapping with a multi-char key.UserDict({"A": "PersonA@123.com", "B": "PersonB@123.com"}).Both fail without the fix (mangled output /
ValueError) and pass with it.Validation run locally:
filterwarnings=error).ruff checkandruff format --check: clean on changed files.mypy(1.15.0, the pinned CI version): success.A
CHANGES.rstentry was added under the unreleased 3.2.0 section.fixes #2269