Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/2928.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed ``AttributeError`` to ``TypeError`` when accessing ``Response.text`` after setting ``body`` to a value that becomes a ``Payload`` without a ``decode`` method.
11 changes: 7 additions & 4 deletions aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,10 +636,13 @@ def body(self, body: Any) -> None:
def text(self) -> str | None:
if self._body is None:
return None
# Note: When _body is a Payload (e.g. FilePayload), this may do blocking I/O
# This is generally safe as most common payloads (BytesPayload, StringPayload)
# don't do blocking I/O, but be careful with file-based payloads
return self._body.decode(self.charset or "utf-8")
try:
return self._body.decode(self.charset or "utf-8")
except AttributeError:
raise TypeError(
f"Unable to get text from body type {type(self._body).__name__}. "
f"Use the text setter or pass text= to Response() for string content."
)

@text.setter
def text(self, text: str) -> None:
Expand Down
Loading