Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pickling HTTPStatusError #3207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
17 changes: 17 additions & 0 deletions httpx/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from __future__ import annotations

import contextlib
import functools
import typing

if typing.TYPE_CHECKING:
Expand Down Expand Up @@ -267,6 +268,22 @@ def __init__(self, message: str, *, request: Request, response: Response) -> Non
self.request = request
self.response = response

def __reduce__(self) -> tuple[typing.Any, ...]:
# BaseException has a custom __reduce__ that can't handle subclasses with
# required keyword-only arguments.
return (
functools.partial(
self.__class__, request=self.request, response=self.response
),
self.args,
# In case user code adds attributes to the exception instance.
{
k: v
for k, v in self.__dict__.items()
if k not in ("_request", "response")
},
)


class InvalidURL(Exception):
"""
Expand Down
17 changes: 17 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import pickle
import typing

import httpcore
Expand Down Expand Up @@ -61,3 +62,19 @@ def test_request_attribute() -> None:
request = httpx.Request("GET", "https://www.example.com")
exc = httpx.ReadTimeout("Read operation timed out", request=request)
assert exc.request == request


def test_pickle_error(server):
with httpx.Client() as client:
response = client.request("GET", server.url.copy_with(path="/status/404"))
with pytest.raises(httpx.HTTPStatusError) as exc_info:
response.raise_for_status()
error = exc_info.value
assert isinstance(error, httpx.HTTPStatusError)
pickled_error = pickle.dumps(error)
unpickled_error = pickle.loads(pickled_error)
# Note that the unpickled error will not be equal to the original error
# because requests and responses are compared by identity.
assert str(unpickled_error) == str(error)
assert unpickled_error.request is not None
assert unpickled_error.response is not None