fix: ensure BodyPartReader.read() returns bytes, not bytearray#12408
fix: ensure BodyPartReader.read() returns bytes, not bytearray#12408NIK-TIGER-BILL wants to merge 3 commits intoaio-libs:masterfrom
Conversation
BodyPartReader.read() internally accumulates data into a bytearray for efficient concatenation, but returns it directly, violating the annotated return type of . This causes downstream code that expects (e.g. json.dumps) to fail with TypeError. Fix: wrap both return paths (raw and decoded) with bytes() to ensure the returned value always matches the type annotation. Fixes aio-libs#12404 Signed-off-by: NIK-TIGER-BILL <[email protected]>
Signed-off-by: NIK-TIGER-BILL <[email protected]>
for more information, see https://pre-commit.ci
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #12408 +/- ##
=======================================
Coverage 98.92% 98.92%
=======================================
Files 134 134
Lines 46741 46756 +15
Branches 2429 2429
=======================================
+ Hits 46239 46254 +15
Misses 373 373
Partials 129 129
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 degrade performance by 9.81%
Performance Changes
Comparing Footnotes
|
|
Thanks for the performance report @codspeed-hq. The ~9.8% regression on The affected benchmark measures @Dreamsorcerer explicitly requested this fix in the original issue (#12404). Happy to discuss alternatives if the maintainers prefer a different approach. |
|
The PR description clearly repeats the disputed details in the bug report, suggesting no person has actually looked at the issue.. |
What do these changes do?
BodyPartReader.read()internally accumulates data into abytearrayfor efficient concatenation, but returns it directly, violating the annotated return type ofbytes. This causes downstream code that expectsbytes(e.g.json.dumps) to fail withTypeError: Object of type bytearray is not JSON serializable.This PR wraps both return paths (raw and decoded) with
bytes()to ensure the returned value always matches the type annotation.Are there changes in behavior for the user?
Yes —
read()andread(decode=True)now returnbytesinstead ofbytearray. This is a fix to match the documented type; code that already treated the return asbyteswill see no difference. Code that relied on the mutablebytearray(unlikely given the type hints) would need adjustment.Is it a substantial burden for the maintainers to support this?
No. The change is minimal (two
bytes()calls) and does not introduce new API surface or complexity.Related issue number
Fixes #12404
Checklist
CONTRIBUTORS.txtCHANGES/folder