Skip to content

chore: disallow incomplete type defs #1342

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

Merged
merged 1 commit into from
Feb 28, 2021
Merged
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
4 changes: 4 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
[mypy]
files = gitlab/*.py

# disallow_incomplete_defs: This flag reports an error whenever it encounters a
# partly annotated function definition.
disallow_incomplete_defs = True
12 changes: 8 additions & 4 deletions gitlab/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __getattr__(self, name: str) -> Any:
except KeyError:
raise AttributeError(name)

def __setattr__(self, name: str, value) -> None:
def __setattr__(self, name: str, value: Any) -> None:
self.__dict__["_updated_attrs"][name] = value

def __str__(self) -> str:
Expand All @@ -116,12 +116,16 @@ def __repr__(self) -> str:
else:
return "<%s>" % self.__class__.__name__

def __eq__(self, other) -> bool:
def __eq__(self, other: object) -> bool:
if not isinstance(other, RESTObject):
return NotImplemented
if self.get_id() and other.get_id():
return self.get_id() == other.get_id()
return super(RESTObject, self) == other

def __ne__(self, other) -> bool:
def __ne__(self, other: object) -> bool:
if not isinstance(other, RESTObject):
return NotImplemented
if self.get_id() and other.get_id():
return self.get_id() != other.get_id()
return super(RESTObject, self) != other
Expand All @@ -144,7 +148,7 @@ def _create_managers(self) -> None:
manager = cls(self.manager.gitlab, parent=self)
self.__dict__[attr] = manager

def _update_attrs(self, new_attrs) -> None:
def _update_attrs(self, new_attrs: Dict[str, Any]) -> None:
self.__dict__["_updated_attrs"] = {}
self.__dict__["_attrs"] = new_attrs

Expand Down
19 changes: 11 additions & 8 deletions gitlab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import functools
import re
import sys
from typing import Any, Callable, Dict, Tuple
from typing import Any, Callable, Dict, Optional, Tuple, Union

import gitlab.config

Expand All @@ -32,21 +32,24 @@
# action: (mandatory_args, optional_args, in_obj),
# },
# }
custom_actions: Dict[str, Dict[str, Tuple[Tuple[Any, ...], Tuple[Any, ...], bool]]] = {}
custom_actions: Dict[str, Dict[str, Tuple[Tuple[str, ...], Tuple[str, ...], bool]]] = {}


def register_custom_action(
cls_names, mandatory: Tuple[Any, ...] = tuple(), optional: Tuple[Any, ...] = tuple()
cls_names: Union[str, Tuple[str, ...]],
mandatory: Tuple[str, ...] = tuple(),
optional: Tuple[str, ...] = tuple(),
) -> Callable:
def wrap(f) -> Callable:
def wrap(f: Callable) -> Callable:
@functools.wraps(f)
def wrapped_f(*args, **kwargs):
return f(*args, **kwargs)

# in_obj defines whether the method belongs to the obj or the manager
in_obj = True
classes = cls_names
if type(cls_names) != tuple:
if isinstance(cls_names, tuple):
classes = cls_names
else:
classes = (cls_names,)

for cls_name in classes:
Expand All @@ -65,7 +68,7 @@ def wrapped_f(*args, **kwargs):
return wrap


def die(msg: str, e=None) -> None:
def die(msg: str, e: Optional[Exception] = None) -> None:
if e:
msg = "%s (%s)" % (msg, e)
sys.stderr.write(msg + "\n")
Expand All @@ -76,7 +79,7 @@ def what_to_cls(what: str) -> str:
return "".join([s.capitalize() for s in what.split("-")])


def cls_to_what(cls) -> str:
def cls_to_what(cls: Any) -> str:
return camel_re.sub(r"\1-\2", cls.__name__).lower()


Expand Down
36 changes: 19 additions & 17 deletions gitlab/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def __init__(
def __enter__(self) -> "Gitlab":
return self

def __exit__(self, *args) -> None:
def __exit__(self, *args: Any) -> None:
self.session.close()

def __getstate__(self) -> Dict[str, Any]:
Expand Down Expand Up @@ -180,7 +180,9 @@ def api_version(self) -> str:
return self._api_version

@classmethod
def from_config(cls, gitlab_id=None, config_files=None) -> "Gitlab":
def from_config(
cls, gitlab_id: Optional[str] = None, config_files: Optional[List[str]] = None
) -> "Gitlab":
"""Create a Gitlab connection from configuration files.

Args:
Expand Down Expand Up @@ -247,7 +249,7 @@ def version(self) -> Tuple[str, str]:
return cast(str, self._server_version), cast(str, self._server_revision)

@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabVerifyError)
def lint(self, content: str, **kwargs) -> Tuple[bool, List[str]]:
def lint(self, content: str, **kwargs: Any) -> Tuple[bool, List[str]]:
"""Validate a gitlab CI configuration.

Args:
Expand All @@ -269,7 +271,7 @@ def lint(self, content: str, **kwargs) -> Tuple[bool, List[str]]:

@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabMarkdownError)
def markdown(
self, text: str, gfm: bool = False, project: Optional[str] = None, **kwargs
self, text: str, gfm: bool = False, project: Optional[str] = None, **kwargs: Any
) -> str:
"""Render an arbitrary Markdown document.

Expand All @@ -296,7 +298,7 @@ def markdown(
return data["html"]

@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabLicenseError)
def get_license(self, **kwargs) -> Dict[str, Any]:
def get_license(self, **kwargs: Any) -> Dict[str, Any]:
"""Retrieve information about the current license.

Args:
Expand All @@ -315,7 +317,7 @@ def get_license(self, **kwargs) -> Dict[str, Any]:
return {}

@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabLicenseError)
def set_license(self, license: str, **kwargs) -> Dict[str, Any]:
def set_license(self, license: str, **kwargs: Any) -> Dict[str, Any]:
"""Add a new license.

Args:
Expand Down Expand Up @@ -446,7 +448,7 @@ def http_request(
post_data: Optional[Dict[str, Any]] = None,
streamed: bool = False,
files: Optional[Dict[str, Any]] = None,
**kwargs,
**kwargs: Any,
) -> requests.Response:
"""Make an HTTP request to the Gitlab server.

Expand Down Expand Up @@ -577,7 +579,7 @@ def http_get(
query_data: Optional[Dict[str, Any]] = None,
streamed: bool = False,
raw: bool = False,
**kwargs,
**kwargs: Any,
) -> Union[Dict[str, Any], requests.Response]:
"""Make a GET request to the Gitlab server.

Expand Down Expand Up @@ -621,8 +623,8 @@ def http_list(
self,
path: str,
query_data: Optional[Dict[str, Any]] = None,
as_list=None,
**kwargs,
as_list: Optional[bool] = None,
**kwargs: Any,
) -> Union["GitlabList", List[Dict[str, Any]]]:
"""Make a GET request to the Gitlab server for list-oriented queries.

Expand Down Expand Up @@ -670,7 +672,7 @@ def http_post(
query_data: Optional[Dict[str, Any]] = None,
post_data: Optional[Dict[str, Any]] = None,
files: Optional[Dict[str, Any]] = None,
**kwargs,
**kwargs: Any,
) -> Union[Dict[str, Any], requests.Response]:
"""Make a POST request to the Gitlab server.

Expand Down Expand Up @@ -717,7 +719,7 @@ def http_put(
query_data: Optional[Dict[str, Any]] = None,
post_data: Optional[Dict[str, Any]] = None,
files: Optional[Dict[str, Any]] = None,
**kwargs,
**kwargs: Any,
) -> Union[Dict[str, Any], requests.Response]:
"""Make a PUT request to the Gitlab server.

Expand Down Expand Up @@ -755,7 +757,7 @@ def http_put(
error_message="Failed to parse the server message"
) from e

def http_delete(self, path: str, **kwargs) -> requests.Response:
def http_delete(self, path: str, **kwargs: Any) -> requests.Response:
"""Make a PUT request to the Gitlab server.

Args:
Expand All @@ -773,7 +775,7 @@ def http_delete(self, path: str, **kwargs) -> requests.Response:

@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabSearchError)
def search(
self, scope: str, search: str, **kwargs
self, scope: str, search: str, **kwargs: Any
) -> Union["GitlabList", List[Dict[str, Any]]]:
"""Search GitLab resources matching the provided string.'

Expand Down Expand Up @@ -806,7 +808,7 @@ def __init__(
url: str,
query_data: Dict[str, Any],
get_next: bool = True,
**kwargs,
**kwargs: Any,
) -> None:
self._gl = gl

Expand All @@ -817,7 +819,7 @@ def __init__(
self._get_next = get_next

def _query(
self, url: str, query_data: Optional[Dict[str, Any]] = None, **kwargs
self, url: str, query_data: Optional[Dict[str, Any]] = None, **kwargs: Any
) -> None:
query_data = query_data or {}
result = self._gl.http_request("get", url, query_data=query_data, **kwargs)
Expand All @@ -842,7 +844,7 @@ def _query(
self._total: Optional[Union[str, int]] = result.headers.get("X-Total")

try:
self._data = result.json()
self._data: List[Dict[str, Any]] = result.json()
except Exception as e:
raise gitlab.exceptions.GitlabParsingError(
error_message="Failed to parse the server message"
Expand Down
5 changes: 3 additions & 2 deletions gitlab/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


class _StdoutStream(object):
def __call__(self, chunk) -> None:
def __call__(self, chunk: Any) -> None:
print(chunk)


Expand All @@ -31,7 +31,7 @@ def response_content(
streamed: bool,
action: Optional[Callable],
chunk_size: int,
):
) -> Optional[bytes]:
if streamed is False:
return response.content

Expand All @@ -41,6 +41,7 @@ def response_content(
for chunk in response.iter_content(chunk_size=chunk_size):
if chunk:
action(chunk)
return None


def copy_dict(dest: Dict[str, Any], src: Dict[str, Any]) -> None:
Expand Down