Skip to content

Commit 9415af6

Browse files
Make raise_for_status chainable (#2776)
* merge upstream * lint * Update test_async_client.py * update docs * add example * Update docs/quickstart.md Co-authored-by: Tom Christie <tom@tomchristie.com> * Update CHANGELOG.md Co-authored-by: Tom Christie <tom@tomchristie.com> * Update docs/quickstart.md Co-authored-by: Tom Christie <tom@tomchristie.com> --------- Co-authored-by: Tom Christie <tom@tomchristie.com>
1 parent 55b8669 commit 9415af6

6 files changed

Lines changed: 14 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
99
### Added
1010

1111
* Add `socket_options` argument to `httpx.HTTPTransport` and `httpx.AsyncHTTPTransport` classes. (#2716)
12+
* The `Response.raise_for_status()` method now returns the response instance. For example: `data = httpx.get('...').raise_for_status().json()`. (#2776)
1213

1314
### Fixed
1415

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
* The amount of time elapsed between sending the request and calling `close()` on the corresponding response received for that request.
7171
[total_seconds()](https://docs.python.org/3/library/datetime.html#datetime.timedelta.total_seconds) to correctly get
7272
the total elapsed seconds.
73-
* `def .raise_for_status()` - **None**
73+
* `def .raise_for_status()` - **Response**
7474
* `def .json()` - **Any**
7575
* `def .read()` - **bytes**
7676
* `def .iter_raw([chunk_size])` - **bytes iterator**

docs/quickstart.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,19 @@ httpx._exceptions.HTTPStatusError: 404 Client Error: Not Found for url: https://
288288
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404
289289
```
290290

291-
Any successful response codes will simply return `None` rather than raising an exception.
291+
Any successful response codes will return the `Response` instance rather than raising an exception.
292292

293293
```pycon
294294
>>> r.raise_for_status()
295295
```
296296

297+
The method returns the response instance, allowing you to use it inline. For example:
298+
299+
```pycon
300+
>>> r = httpx.get('...').raise_for_status()
301+
>>> data = httpx.get('...').raise_for_status().json()
302+
```
303+
297304
## Response Headers
298305

299306
The response headers are available as a dictionary-like interface.

httpx/_models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ def has_redirect_location(self) -> bool:
711711
and "Location" in self.headers
712712
)
713713

714-
def raise_for_status(self) -> None:
714+
def raise_for_status(self) -> "Response":
715715
"""
716716
Raise the `HTTPStatusError` if one occurred.
717717
"""
@@ -723,7 +723,7 @@ def raise_for_status(self) -> None:
723723
)
724724

725725
if self.is_success:
726-
return
726+
return self
727727

728728
if self.has_redirect_location:
729729
message = (

tests/client/test_async_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ async def test_raise_for_status(server):
122122
response.raise_for_status()
123123
assert exc_info.value.response == response
124124
else:
125-
assert response.raise_for_status() is None # type: ignore
125+
assert response.raise_for_status() is response
126126

127127

128128
@pytest.mark.anyio

tests/client/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def test_raise_for_status(server):
141141
assert exc_info.value.response == response
142142
assert exc_info.value.request.url.path == f"/status/{status_code}"
143143
else:
144-
assert response.raise_for_status() is None # type: ignore
144+
assert response.raise_for_status() is response
145145

146146

147147
def test_options(server):

0 commit comments

Comments
 (0)