Skip to content

Fix Response.text failing when body is set to a string#12138

Open
dhruvildarji wants to merge 1 commit intoaio-libs:masterfrom
dhruvildarji:fix/response-text-attribute-error
Open

Fix Response.text failing when body is set to a string#12138
dhruvildarji wants to merge 1 commit intoaio-libs:masterfrom
dhruvildarji:fix/response-text-attribute-error

Conversation

@dhruvildarji
Copy link
Contributor

Summary

Setting Response.body to a string and then accessing Response.text would fail with an AttributeError because the body setter created a StringPayload wrapper that didn't always have a compatible decode method.

The fix handles str explicitly in the body setter by encoding it directly to bytes, consistent with how the text setter works. It also sets sensible defaults for content_type and charset when they haven't been configured.

What's Changed

  • Added str handling in Response.body setter that encodes to bytes using the configured charset
  • Sets content_type to text/plain if it was the default application/octet-stream
  • Sets charset to utf-8 if not already set
  • Added tests for the new behavior

Test Plan

  • Setting body to str stores bytes and text property works
  • Content-type defaults are set correctly
  • Existing content-type is preserved
  • Existing charset is used for encoding
  • Roundtrip body→text→body is consistent
  • All existing tests pass

Fixes #2928

Handle str in the body setter by encoding it to bytes directly,
mirroring the behavior of the text setter. Previously, setting
body to a string would create a StringPayload, causing .text to
fail with an AttributeError.

Fixes aio-libs#2928
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Feb 24, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 24, 2026

Merging this PR will not alter performance

✅ 59 untouched benchmarks


Comparing dhruvildarji:fix/response-text-attribute-error (7fc4718) with master (4ec897b)

Open in CodSpeed

@codecov
Copy link

codecov bot commented Feb 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.78%. Comparing base (f049588) to head (7fc4718).
⚠️ Report is 5 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12138      +/-   ##
==========================================
- Coverage   98.78%   98.78%   -0.01%     
==========================================
  Files         128      128              
  Lines       45297    45330      +33     
  Branches     2403     2406       +3     
==========================================
+ Hits        44748    44780      +32     
  Misses        390      390              
- Partials      159      160       +1     
Flag Coverage Δ
CI-GHA 98.64% <100.00%> (-0.01%) ⬇️
OS-Linux 98.38% <100.00%> (+<0.01%) ⬆️
OS-Windows 96.75% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.63% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 97.18% <100.00%> (+<0.01%) ⬆️
Py-3.10.19 97.66% <100.00%> (+<0.01%) ⬆️
Py-3.11.14 97.85% <100.00%> (-0.01%) ⬇️
Py-3.11.9 97.39% <100.00%> (-0.01%) ⬇️
Py-3.12.10 97.48% <100.00%> (-0.01%) ⬇️
Py-3.12.12 97.95% <100.00%> (+<0.01%) ⬆️
Py-3.13.11 97.95% <100.00%> (+<0.01%) ⬆️
Py-3.13.12 97.46% <100.00%> (-0.01%) ⬇️
Py-3.14.2 97.93% <100.00%> (+<0.01%) ⬆️
Py-3.14.3 97.42% <100.00%> (+<0.01%) ⬆️
Py-3.14.3t 97.26% <100.00%> (-0.01%) ⬇️
Py-pypy3.11.13-7.3.20 97.30% <100.00%> (-0.01%) ⬇️
VM-macos 97.63% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 98.38% <100.00%> (+<0.01%) ⬆️
VM-windows 96.75% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 654 to 660
if self.content_type == "application/octet-stream":
self.content_type = "text/plain"
if self.charset is None:
self.charset = "utf-8"

self._body = text.encode(self.charset)
self._compressed_body = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not just make this:
self.body = text

And avoid the code duplication?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created a StringPayload wrapper that didn't always have a compatible decode method.

Under what condition does it not have a compatible decode() method? It looks like it should work to me...

@webknjaz
Copy link
Member

webknjaz commented Feb 24, 2026

Could you fill out the PR template?

Also, why does this cause a coverage drop in the http writer @ https://app.codecov.io/gh/aio-libs/aiohttp/pull/12138/indirect-changes#b01a901427ef6052963c0c1281a51872-R212 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Accessing Response.text can fail with an AttributeError

3 participants