diff options
author | John L. Villalovos <john@sodarock.com> | 2021-04-18 12:06:07 -0700 |
---|---|---|
committer | John L. Villalovos <john@sodarock.com> | 2021-04-27 21:04:58 -0700 |
commit | 6aef2dadf715e601ae9c302be0ad9958345a97f2 (patch) | |
tree | d254ecefd18029eb15d28bec0f9293caa4219e6b /gitlab/exceptions.py | |
parent | a6b6cd4b598ab6eddcf3986486d43e5cdc990e09 (diff) | |
download | gitlab-6aef2dadf715e601ae9c302be0ad9958345a97f2.tar.gz |
chore: mypy: Disallow untyped definitions
Be more strict and don't allow untyped definitions on the files we
check.
Also this adds type-hints for two of the decorators so that now
functions/methods decorated by them will have their types be revealed
correctly.
Diffstat (limited to 'gitlab/exceptions.py')
-rw-r--r-- | gitlab/exceptions.py | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py index f5b3600..77a7492 100644 --- a/gitlab/exceptions.py +++ b/gitlab/exceptions.py @@ -16,10 +16,16 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import functools +from typing import Any, Callable, cast, Optional, Type, TypeVar, TYPE_CHECKING, Union class GitlabError(Exception): - def __init__(self, error_message="", response_code=None, response_body=None): + def __init__( + self, + error_message: Union[str, bytes] = "", + response_code: Optional[int] = None, + response_body: Optional[bytes] = None, + ) -> None: Exception.__init__(self, error_message) # Http status code @@ -30,11 +36,15 @@ class GitlabError(Exception): try: # if we receive str/bytes we try to convert to unicode/str to have # consistent message types (see #616) + if TYPE_CHECKING: + assert isinstance(error_message, bytes) self.error_message = error_message.decode() except Exception: + if TYPE_CHECKING: + assert isinstance(error_message, str) self.error_message = error_message - def __str__(self): + def __str__(self) -> str: if self.response_code is not None: return "{0}: {1}".format(self.response_code, self.error_message) else: @@ -269,7 +279,14 @@ class GitlabUnfollowError(GitlabOperationError): pass -def on_http_error(error): +# For an explanation of how these type-hints work see: +# https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators +# +# The goal here is that functions which get decorated will retain their types. +__F = TypeVar("__F", bound=Callable[..., Any]) + + +def on_http_error(error: Type[Exception]) -> Callable[[__F], __F]: """Manage GitlabHttpError exceptions. This decorator function can be used to catch GitlabHttpError exceptions @@ -280,14 +297,14 @@ def on_http_error(error): GitlabError """ - def wrap(f): + def wrap(f: __F) -> __F: @functools.wraps(f) - def wrapped_f(*args, **kwargs): + def wrapped_f(*args: Any, **kwargs: Any) -> Any: try: return f(*args, **kwargs) except GitlabHttpError as e: raise error(e.error_message, e.response_code, e.response_body) from e - return wrapped_f + return cast(__F, wrapped_f) return wrap |