]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Improve type hinting for `HTTPError.request` (#886)
authorFlorimond Manca <florimond.manca@gmail.com>
Sun, 29 Mar 2020 11:12:05 +0000 (13:12 +0200)
committerGitHub <noreply@github.com>
Sun, 29 Mar 2020 11:12:05 +0000 (13:12 +0200)
httpx/_client.py
httpx/_exceptions.py

index 397652231c2cf44fab3fa11dab6580984e1f0a83..50279b0d531b0674d653f2eccbb7908923a9f91c 100644 (file)
@@ -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)
index d1597c078d2cc31d084ed25d378756785f97ae02..9b6bd22ca684ddb07f408dba30d461cf5afc4163 100644 (file)
@@ -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...