fix(requests): fall back to stdlib json in Response.json() when kwargs are passed (#639) - #796
Open
apoorvdarshan wants to merge 1 commit into
Open
Conversation
…s are passed Response.json(**kw) forwards keyword arguments to loads(), but loads resolves to orjson.loads when orjson is installed, and orjson.loads() accepts no keyword arguments. This made calls like response.json(parse_float=Decimal) raise TypeError when orjson is present, while working fine when it is absent. Use stdlib json.loads whenever the caller passes keyword arguments, and keep the faster orjson.loads for the common no-kwargs case, so behavior no longer depends on whether orjson happens to be installed. Fixes lexiforest#639
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 #639
Root cause
Response.json(**kw)forwards its keyword arguments straight toloads():In
curl_cffi/requests/models.py,loadsis bound toorjson.loadswhen orjson is installed and tojson.loadsotherwise.orjson.loads()accepts no keyword arguments, whereas the stdlibjson.loads()does. A call such asresponse.json(parse_float=Decimal)therefore raisesTypeErroronly when orjson is installed.Fix
Keep the faster
orjson.loadsfor the common no-kwargs case, but fall back to the stdlibjson.loadswhenever the caller passes keyword arguments. The no-kwargs fast path is unchanged.Tests
Added
test_json_kwargs_with_orjson, which verifies thatr.json(parse_float=Decimal)returns aDecimalwhen orjson is installed.TypeError.ruff checkandruff format --checkare clean on the changed files.Review checklist
Disclosure: prepared with AI assistance; manually reviewed and verified locally.