Fix BodyPartReader.read() returning bytearray instead of bytes#12671
Open
jesustorres-code wants to merge 1 commit into
Open
Fix BodyPartReader.read() returning bytearray instead of bytes#12671jesustorres-code wants to merge 1 commit into
jesustorres-code wants to merge 1 commit into
Conversation
The read() method accumulated data in a bytearray internally but returned it without converting to bytes, violating the return type annotation and breaking downstream code that relies on bytes (e.g. json.dumps serialization). Both code paths (decode=True and the plain path) now return bytes(). Fixes aio-libs#12404
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #12671 +/- ##
==========================================
- Coverage 98.95% 98.56% -0.39%
==========================================
Files 131 131
Lines 46688 46696 +8
Branches 2421 2421
==========================================
- Hits 46200 46026 -174
- Misses 366 526 +160
- Partials 122 144 +22
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Merging this PR will not alter performance
Comparing Footnotes
|
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 #12404
Problem
BodyPartReader.read()accumulates data in abytearrayinternally and returns it directly, violating the-> bytesreturn type annotation. Withdecode=Truethe decoded accumulation buffer also returns asbytearray.This causes downstream failures like
TypeError: Object of type bytearray is not JSON serializablewhen the result is passed tojson.dumps, and silently breaks code that checksisinstance(result, bytes).Fix
Convert the internal
bytearraytobytesat the return point in both code paths:The
bytearrayis still used internally for efficient.extend()accumulation; only the final return value changes.Tests
assert type(result) is bytesto the existingtest_readtesttest_read_returns_bytes_not_bytearray_with_decodecovering thedecode=Truepath withContent-Encoding: identity