From: Florimond Manca Date: Sun, 29 Mar 2020 11:12:05 +0000 (+0200) Subject: Improve type hinting for `HTTPError.request` (#886) X-Git-Tag: 0.13.0.dev0~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1c9b852783f065db1b495b587c1c986246355198;p=thirdparty%2Fhttpx.git Improve type hinting for `HTTPError.request` (#886) --- diff --git a/httpx/_client.py b/httpx/_client.py index 39765223..50279b0d 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -680,8 +680,8 @@ class Client(BaseClient): # Add the original request to any HTTPError unless # there'a already a request attached in the case of # a ProxyError. - if exc.request is None: - exc.request = request + if exc._request is None: + exc._request = request raise self.cookies.extract_cookies(response) @@ -1198,8 +1198,8 @@ class AsyncClient(BaseClient): # Add the original request to any HTTPError unless # there'a already a request attached in the case of # a ProxyError. - if exc.request is None: - exc.request = request + if exc._request is None: + exc._request = request raise self.cookies.extract_cookies(response) diff --git a/httpx/_exceptions.py b/httpx/_exceptions.py index d1597c07..9b6bd22c 100644 --- a/httpx/_exceptions.py +++ b/httpx/_exceptions.py @@ -12,9 +12,16 @@ class HTTPError(Exception): def __init__( self, *args: typing.Any, request: "Request" = None, response: "Response" = None ) -> None: - self.response = response - self.request = request or getattr(self.response, "request", None) super().__init__(*args) + self._request = request or (response.request if response is not None else None) + self.response = response + + @property + def request(self) -> "Request": + # NOTE: this property exists so that a `Request` is exposed to type + # checkers, instead of `Optional[Request]`. + assert self._request is not None # Populated by the client. + return self._request # Timeout exceptions...