Fix AttributeError when accessing Response.text with Payload body#12239
Fix AttributeError when accessing Response.text with Payload body#12239wavebyrd wants to merge 3 commits intoaio-libs:masterfrom
Conversation
When Response.body is set to a value that gets converted to a Payload without a decode method (e.g., AsyncIterablePayload, BodyPartReader), accessing Response.text would raise an AttributeError. Now properly checks for the decode method and raises a clear TypeError with guidance on using the text setter for string content. Fixes aio-libs#2928 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #12239 +/- ##
==========================================
- Coverage 99.11% 99.10% -0.01%
==========================================
Files 130 130
Lines 45373 45407 +34
Branches 2397 2400 +3
==========================================
+ Hits 44970 45002 +32
- Misses 271 274 +3
+ Partials 132 131 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
|
Ping - this fixes AttributeError when Response.text is accessed with Payload body. Let me know if any changes needed! |
Do you not see the comment above? |
Address review feedback: use try/except to catch AttributeError instead of isinstance/hasattr checks, which is more Pythonic and has no performance overhead when successful. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Thanks for the review feedback! I've updated to use the try/except pattern - much cleaner. The isinstance check for bytes/bytearray was redundant since they have decode() anyway. |
|
Can we get a test to cover the new code? |
Summary
When
Response.bodyis set to a value that gets converted to aPayloadwithout adecodemethod (e.g.,AsyncIterablePayload,BodyPartReader), accessingResponse.textwould raise an unhelpfulAttributeError:Changes
Response.textproperty to check forbytes/bytearrayfirstPayloadobjects, checks ifdecodemethod exists before calling itTypeErrorwith guidance when the body type doesn't support text accessTest Plan
test_payload_body_get_textalready cover this caseTypeErrorfor payloads likeasync_iter()andBodyPartReaderFixes #2928