From 469772652ef1e094e4779c4abb895f1eb7d96f43 Mon Sep 17 00:00:00 2001 From: Alessandro Dalvit Date: Mon, 16 Oct 2023 14:48:51 +0200 Subject: [PATCH] chore: make mypy@github happy --- mapillary_tools/authenticate.py | 6 +++++- mapillary_tools/upload.py | 19 +++++++++++++------ mapillary_tools/uploader.py | 10 ++++++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/mapillary_tools/authenticate.py b/mapillary_tools/authenticate.py index b13965cb..4b80b46a 100644 --- a/mapillary_tools/authenticate.py +++ b/mapillary_tools/authenticate.py @@ -51,7 +51,11 @@ def prompt_user_for_user_items(user_name: str) -> types.UserItem: try: resp = api_v4.get_upload_token(user_email, user_password) except requests.HTTPError as ex: - if 400 <= ex.response.status_code < 500: + if ( + isinstance(ex, requests.HTTPError) + and isinstance(ex.response, requests.Response) + and 400 <= ex.response.status_code < 500 + ): r = ex.response.json() subcode = r.get("error", {}).get("error_subcode") if subcode in [1348028, 1348092, 3404005, 1348131]: diff --git a/mapillary_tools/upload.py b/mapillary_tools/upload.py index e63552d7..3e46a117 100644 --- a/mapillary_tools/upload.py +++ b/mapillary_tools/upload.py @@ -62,12 +62,17 @@ class DirectUploadFileType(enum.Enum): def wrap_http_exception(ex: requests.HTTPError): + req = ex.request resp = ex.response - lines = [ - f"{ex.request.method} {resp.url}", - f"> HTTP Status: {ex.response.status_code}", - str(resp.content), - ] + if isinstance(resp, requests.Response) and isinstance(req, requests.Request): + lines = [ + f"{req.method} {resp.url}", + f"> HTTP Status: {resp.status_code}", + str(resp.content), + ] + else: + lines = [] + return UploadHTTPError("\n".join(lines)) @@ -713,7 +718,9 @@ def upload( if isinstance(inner_ex, requests.Timeout): raise exceptions.MapillaryUploadTimeoutError(str(inner_ex)) from inner_ex - if isinstance(inner_ex, requests.HTTPError): + if isinstance(inner_ex, requests.HTTPError) and isinstance( + inner_ex.response, requests.Response + ): if inner_ex.response.status_code in [400, 401]: try: error_body = inner_ex.response.json() diff --git a/mapillary_tools/uploader.py b/mapillary_tools/uploader.py index 0c644269..b69c99fb 100644 --- a/mapillary_tools/uploader.py +++ b/mapillary_tools/uploader.py @@ -321,7 +321,11 @@ def _extract_upload_md5sum(fp: T.IO[bytes]) -> T.Optional[str]: def _is_immediate_retry(ex: Exception): - if isinstance(ex, requests.HTTPError) and ex.response.status_code == 412: + if ( + isinstance(ex, requests.HTTPError) + and isinstance(ex.response, requests.Response) + and ex.response.status_code == 412 + ): try: resp = ex.response.json() except json.JSONDecodeError: @@ -334,7 +338,9 @@ def _is_retriable_exception(ex: Exception): if isinstance(ex, (requests.ConnectionError, requests.Timeout)): return True - if isinstance(ex, requests.HTTPError): + if isinstance(ex, requests.HTTPError) and isinstance( + ex.response, requests.Response + ): if 400 <= ex.response.status_code < 500: try: resp = ex.response.json()