The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
+## Unreleased
+
+### Fixed
+
+* Raise `ValueError` on `Response.encoding` being set after `Response.text` has been accessed. (#2852)
+
## 0.25.0 (11th Sep, 2023)
### Removed
@encoding.setter
def encoding(self, value: str) -> None:
+ """
+ Set the encoding to use for decoding the byte content into text.
+
+ If the `text` attribute has been accessed, attempting to set the
+ encoding will throw a ValueError.
+ """
+ if hasattr(self, "_text"):
+ raise ValueError(
+ "Setting encoding after `text` has been accessed is not allowed."
+ )
self._encoding = value
@property
assert response.encoding == "iso-8859-1"
+def test_response_force_encoding_after_text_accessed():
+ response = httpx.Response(
+ 200,
+ content=b"Hello, world!",
+ )
+ assert response.status_code == 200
+ assert response.reason_phrase == "OK"
+ assert response.text == "Hello, world!"
+ assert response.encoding == "utf-8"
+
+ with pytest.raises(ValueError):
+ response.encoding = "UTF8"
+
+ with pytest.raises(ValueError):
+ response.encoding = "iso-8859-1"
+
+
def test_read():
response = httpx.Response(
200,