Skip to content

Commit 472b300

Browse files
authored
Merge pull request #1680 from python-gitlab/jlvillal/mypy_small_files_1
chore: enforce type-hints on most files in gitlab/v4/objects/
2 parents 9a2f54c + 7828ba2 commit 472b300

22 files changed

+232
-58
lines changed

gitlab/client.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,10 @@ def _check_redirects(self, result: requests.Response) -> None:
495495
def _prepare_send_data(
496496
self,
497497
files: Optional[Dict[str, Any]] = None,
498-
post_data: Optional[Dict[str, Any]] = None,
498+
post_data: Optional[Union[Dict[str, Any], bytes]] = None,
499499
raw: bool = False,
500500
) -> Tuple[
501-
Optional[Dict[str, Any]],
501+
Optional[Union[Dict[str, Any], bytes]],
502502
Optional[Union[Dict[str, Any], MultipartEncoder]],
503503
str,
504504
]:
@@ -508,6 +508,8 @@ def _prepare_send_data(
508508
else:
509509
# booleans does not exists for data (neither for MultipartEncoder):
510510
# cast to string int to avoid: 'bool' object has no attribute 'encode'
511+
if TYPE_CHECKING:
512+
assert isinstance(post_data, dict)
511513
for k, v in post_data.items():
512514
if isinstance(v, bool):
513515
post_data[k] = str(int(v))
@@ -527,7 +529,7 @@ def http_request(
527529
verb: str,
528530
path: str,
529531
query_data: Optional[Dict[str, Any]] = None,
530-
post_data: Optional[Dict[str, Any]] = None,
532+
post_data: Optional[Union[Dict[str, Any], bytes]] = None,
531533
raw: bool = False,
532534
streamed: bool = False,
533535
files: Optional[Dict[str, Any]] = None,
@@ -544,7 +546,7 @@ def http_request(
544546
path (str): Path or full URL to query ('/projects' or
545547
'http://whatever/v4/api/projecs')
546548
query_data (dict): Data to send as query parameters
547-
post_data (dict): Data to send in the body (will be converted to
549+
post_data (dict|bytes): Data to send in the body (will be converted to
548550
json by default)
549551
raw (bool): If True, do not convert post_data to json
550552
streamed (bool): Whether the data should be streamed
@@ -800,7 +802,7 @@ def http_put(
800802
self,
801803
path: str,
802804
query_data: Optional[Dict[str, Any]] = None,
803-
post_data: Optional[Dict[str, Any]] = None,
805+
post_data: Optional[Union[Dict[str, Any], bytes]] = None,
804806
raw: bool = False,
805807
files: Optional[Dict[str, Any]] = None,
806808
**kwargs: Any,
@@ -811,7 +813,7 @@ def http_put(
811813
path (str): Path or full URL to query ('/projects' or
812814
'http://whatever/v4/api/projecs')
813815
query_data (dict): Data to send as query parameters
814-
post_data (dict): Data to send in the body (will be converted to
816+
post_data (dict|bytes): Data to send in the body (will be converted to
815817
json by default)
816818
raw (bool): If True, do not convert post_data to json
817819
files (dict): The files to send to the server

gitlab/v4/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ def do_project_export_download(self) -> None:
103103
if TYPE_CHECKING:
104104
assert export_status is not None
105105
data = export_status.download()
106+
if TYPE_CHECKING:
107+
assert data is not None
106108
sys.stdout.buffer.write(data)
107109

108110
except Exception as e:

gitlab/v4/objects/appearance.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any, cast, Dict, Optional, Union
2+
13
from gitlab import exceptions as exc
24
from gitlab.base import RequiredOptional, RESTManager, RESTObject
35
from gitlab.mixins import GetWithoutIdMixin, SaveMixin, UpdateMixin
@@ -32,7 +34,12 @@ class ApplicationAppearanceManager(GetWithoutIdMixin, UpdateMixin, RESTManager):
3234
)
3335

3436
@exc.on_http_error(exc.GitlabUpdateError)
35-
def update(self, id=None, new_data=None, **kwargs):
37+
def update(
38+
self,
39+
id: Optional[Union[str, int]] = None,
40+
new_data: Dict[str, Any] = None,
41+
**kwargs: Any
42+
) -> Dict[str, Any]:
3643
"""Update an object on the server.
3744
3845
Args:
@@ -49,4 +56,9 @@ def update(self, id=None, new_data=None, **kwargs):
4956
"""
5057
new_data = new_data or {}
5158
data = new_data.copy()
52-
super(ApplicationAppearanceManager, self).update(id, data, **kwargs)
59+
return super(ApplicationAppearanceManager, self).update(id, data, **kwargs)
60+
61+
def get(
62+
self, id: Optional[Union[int, str]] = None, **kwargs: Any
63+
) -> Optional[ApplicationAppearance]:
64+
return cast(ApplicationAppearance, super().get(id=id, **kwargs))

gitlab/v4/objects/badges.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any, cast, Union
2+
13
from gitlab.base import RequiredOptional, RESTManager, RESTObject
24
from gitlab.mixins import BadgeRenderMixin, CRUDMixin, ObjectDeleteMixin, SaveMixin
35

@@ -31,3 +33,8 @@ class ProjectBadgeManager(BadgeRenderMixin, CRUDMixin, RESTManager):
3133
_from_parent_attrs = {"project_id": "id"}
3234
_create_attrs = RequiredOptional(required=("link_url", "image_url"))
3335
_update_attrs = RequiredOptional(optional=("link_url", "image_url"))
36+
37+
def get(
38+
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
39+
) -> ProjectBadge:
40+
return cast(ProjectBadge, super().get(id=id, lazy=lazy, **kwargs))

gitlab/v4/objects/boards.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any, cast, Union
2+
13
from gitlab.base import RequiredOptional, RESTManager, RESTObject
24
from gitlab.mixins import CRUDMixin, ObjectDeleteMixin, SaveMixin
35

@@ -24,6 +26,11 @@ class GroupBoardListManager(CRUDMixin, RESTManager):
2426
_create_attrs = RequiredOptional(required=("label_id",))
2527
_update_attrs = RequiredOptional(required=("position",))
2628

29+
def get(
30+
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
31+
) -> GroupBoardList:
32+
return cast(GroupBoardList, super().get(id=id, lazy=lazy, **kwargs))
33+
2734

2835
class GroupBoard(SaveMixin, ObjectDeleteMixin, RESTObject):
2936
lists: GroupBoardListManager
@@ -35,6 +42,9 @@ class GroupBoardManager(CRUDMixin, RESTManager):
3542
_from_parent_attrs = {"group_id": "id"}
3643
_create_attrs = RequiredOptional(required=("name",))
3744

45+
def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> GroupBoard:
46+
return cast(GroupBoard, super().get(id=id, lazy=lazy, **kwargs))
47+
3848

3949
class ProjectBoardList(SaveMixin, ObjectDeleteMixin, RESTObject):
4050
pass
@@ -47,6 +57,11 @@ class ProjectBoardListManager(CRUDMixin, RESTManager):
4757
_create_attrs = RequiredOptional(required=("label_id",))
4858
_update_attrs = RequiredOptional(required=("position",))
4959

60+
def get(
61+
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
62+
) -> ProjectBoardList:
63+
return cast(ProjectBoardList, super().get(id=id, lazy=lazy, **kwargs))
64+
5065

5166
class ProjectBoard(SaveMixin, ObjectDeleteMixin, RESTObject):
5267
lists: ProjectBoardListManager
@@ -57,3 +72,8 @@ class ProjectBoardManager(CRUDMixin, RESTManager):
5772
_obj_cls = ProjectBoard
5873
_from_parent_attrs = {"project_id": "id"}
5974
_create_attrs = RequiredOptional(required=("name",))
75+
76+
def get(
77+
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
78+
) -> ProjectBoard:
79+
return cast(ProjectBoard, super().get(id=id, lazy=lazy, **kwargs))

gitlab/v4/objects/clusters.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any, cast, Dict, Optional
2+
13
from gitlab import exceptions as exc
24
from gitlab.base import RequiredOptional, RESTManager, RESTObject
35
from gitlab.mixins import CreateMixin, CRUDMixin, ObjectDeleteMixin, SaveMixin
@@ -33,7 +35,9 @@ class GroupClusterManager(CRUDMixin, RESTManager):
3335
)
3436

3537
@exc.on_http_error(exc.GitlabStopError)
36-
def create(self, data, **kwargs):
38+
def create(
39+
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
40+
) -> GroupCluster:
3741
"""Create a new object.
3842
3943
Args:
@@ -51,7 +55,7 @@ def create(self, data, **kwargs):
5155
the data sent by the server
5256
"""
5357
path = f"{self.path}/user"
54-
return CreateMixin.create(self, data, path=path, **kwargs)
58+
return cast(GroupCluster, CreateMixin.create(self, data, path=path, **kwargs))
5559

5660

5761
class ProjectCluster(SaveMixin, ObjectDeleteMixin, RESTObject):
@@ -77,7 +81,9 @@ class ProjectClusterManager(CRUDMixin, RESTManager):
7781
)
7882

7983
@exc.on_http_error(exc.GitlabStopError)
80-
def create(self, data, **kwargs):
84+
def create(
85+
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
86+
) -> ProjectCluster:
8187
"""Create a new object.
8288
8389
Args:
@@ -95,4 +101,4 @@ def create(self, data, **kwargs):
95101
the data sent by the server
96102
"""
97103
path = f"{self.path}/user"
98-
return CreateMixin.create(self, data, path=path, **kwargs)
104+
return cast(ProjectCluster, CreateMixin.create(self, data, path=path, **kwargs))

gitlab/v4/objects/container_registry.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any, TYPE_CHECKING
2+
13
from gitlab import cli
24
from gitlab import exceptions as exc
35
from gitlab.base import RESTManager, RESTObject
@@ -36,7 +38,7 @@ class ProjectRegistryTagManager(DeleteMixin, RetrieveMixin, RESTManager):
3638
optional=("keep_n", "name_regex_keep", "older_than"),
3739
)
3840
@exc.on_http_error(exc.GitlabDeleteError)
39-
def delete_in_bulk(self, name_regex_delete, **kwargs):
41+
def delete_in_bulk(self, name_regex_delete: str, **kwargs: Any) -> None:
4042
"""Delete Tag in bulk
4143
4244
Args:
@@ -55,4 +57,6 @@ def delete_in_bulk(self, name_regex_delete, **kwargs):
5557
valid_attrs = ["keep_n", "name_regex_keep", "older_than"]
5658
data = {"name_regex_delete": name_regex_delete}
5759
data.update({k: v for k, v in kwargs.items() if k in valid_attrs})
60+
if TYPE_CHECKING:
61+
assert self.path is not None
5862
self.gitlab.http_delete(self.path, query_data=data, **kwargs)

gitlab/v4/objects/deploy_keys.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from typing import Any, cast, Dict, Union
2+
3+
import requests
4+
15
from gitlab import cli
26
from gitlab import exceptions as exc
37
from gitlab.base import RequiredOptional, RESTManager, RESTObject
@@ -33,7 +37,9 @@ class ProjectKeyManager(CRUDMixin, RESTManager):
3337

3438
@cli.register_custom_action("ProjectKeyManager", ("key_id",))
3539
@exc.on_http_error(exc.GitlabProjectDeployKeyError)
36-
def enable(self, key_id, **kwargs):
40+
def enable(
41+
self, key_id: int, **kwargs: Any
42+
) -> Union[Dict[str, Any], requests.Response]:
3743
"""Enable a deploy key for a project.
3844
3945
Args:
@@ -43,6 +49,12 @@ def enable(self, key_id, **kwargs):
4349
Raises:
4450
GitlabAuthenticationError: If authentication is not correct
4551
GitlabProjectDeployKeyError: If the key could not be enabled
52+
53+
Returns:
54+
A dict of the result.
4655
"""
4756
path = f"{self.path}/{key_id}/enable"
48-
self.gitlab.http_post(path, **kwargs)
57+
return self.gitlab.http_post(path, **kwargs)
58+
59+
def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> ProjectKey:
60+
return cast(ProjectKey, super().get(id=id, lazy=lazy, **kwargs))

gitlab/v4/objects/environments.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from typing import Any, Dict, Union
2+
3+
import requests
4+
15
from gitlab import cli
26
from gitlab import exceptions as exc
37
from gitlab.base import RequiredOptional, RESTManager, RESTObject
@@ -19,7 +23,7 @@
1923
class ProjectEnvironment(SaveMixin, ObjectDeleteMixin, RESTObject):
2024
@cli.register_custom_action("ProjectEnvironment")
2125
@exc.on_http_error(exc.GitlabStopError)
22-
def stop(self, **kwargs):
26+
def stop(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
2327
"""Stop the environment.
2428
2529
Args:
@@ -28,9 +32,12 @@ def stop(self, **kwargs):
2832
Raises:
2933
GitlabAuthenticationError: If authentication is not correct
3034
GitlabStopError: If the operation failed
35+
36+
Returns:
37+
A dict of the result.
3138
"""
3239
path = f"{self.manager.path}/{self.get_id()}/stop"
33-
self.manager.gitlab.http_post(path, **kwargs)
40+
return self.manager.gitlab.http_post(path, **kwargs)
3441

3542

3643
class ProjectEnvironmentManager(

gitlab/v4/objects/export_import.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any, cast, Optional, Union
2+
13
from gitlab.base import RequiredOptional, RESTManager, RESTObject
24
from gitlab.mixins import CreateMixin, DownloadMixin, GetWithoutIdMixin, RefreshMixin
35

@@ -22,6 +24,11 @@ class GroupExportManager(GetWithoutIdMixin, CreateMixin, RESTManager):
2224
_obj_cls = GroupExport
2325
_from_parent_attrs = {"group_id": "id"}
2426

27+
def get(
28+
self, id: Optional[Union[int, str]] = None, **kwargs: Any
29+
) -> Optional[GroupExport]:
30+
return cast(GroupExport, super().get(id=id, **kwargs))
31+
2532

2633
class GroupImport(RESTObject):
2734
_id_attr = None
@@ -32,6 +39,11 @@ class GroupImportManager(GetWithoutIdMixin, RESTManager):
3239
_obj_cls = GroupImport
3340
_from_parent_attrs = {"group_id": "id"}
3441

42+
def get(
43+
self, id: Optional[Union[int, str]] = None, **kwargs: Any
44+
) -> Optional[GroupImport]:
45+
return cast(GroupImport, super().get(id=id, **kwargs))
46+
3547

3648
class ProjectExport(DownloadMixin, RefreshMixin, RESTObject):
3749
_id_attr = None
@@ -43,6 +55,11 @@ class ProjectExportManager(GetWithoutIdMixin, CreateMixin, RESTManager):
4355
_from_parent_attrs = {"project_id": "id"}
4456
_create_attrs = RequiredOptional(optional=("description",))
4557

58+
def get(
59+
self, id: Optional[Union[int, str]] = None, **kwargs: Any
60+
) -> Optional[ProjectExport]:
61+
return cast(ProjectExport, super().get(id=id, **kwargs))
62+
4663

4764
class ProjectImport(RefreshMixin, RESTObject):
4865
_id_attr = None
@@ -52,3 +69,8 @@ class ProjectImportManager(GetWithoutIdMixin, RESTManager):
5269
_path = "/projects/%(project_id)s/import"
5370
_obj_cls = ProjectImport
5471
_from_parent_attrs = {"project_id": "id"}
72+
73+
def get(
74+
self, id: Optional[Union[int, str]] = None, **kwargs: Any
75+
) -> Optional[ProjectImport]:
76+
return cast(ProjectImport, super().get(id=id, **kwargs))

gitlab/v4/objects/features.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
GitLab API:
3+
https://docs.gitlab.com/ee/api/features.html
4+
"""
5+
from typing import Any, Optional, TYPE_CHECKING, Union
6+
17
from gitlab import exceptions as exc
28
from gitlab import utils
39
from gitlab.base import RESTManager, RESTObject
@@ -20,14 +26,14 @@ class FeatureManager(ListMixin, DeleteMixin, RESTManager):
2026
@exc.on_http_error(exc.GitlabSetError)
2127
def set(
2228
self,
23-
name,
24-
value,
25-
feature_group=None,
26-
user=None,
27-
group=None,
28-
project=None,
29-
**kwargs,
30-
):
29+
name: str,
30+
value: Union[bool, int],
31+
feature_group: Optional[str] = None,
32+
user: Optional[str] = None,
33+
group: Optional[str] = None,
34+
project: Optional[str] = None,
35+
**kwargs: Any,
36+
) -> Feature:
3137
"""Create or update the object.
3238
3339
Args:
@@ -56,4 +62,6 @@ def set(
5662
}
5763
data = utils.remove_none_from_dict(data)
5864
server_data = self.gitlab.http_post(path, post_data=data, **kwargs)
65+
if TYPE_CHECKING:
66+
assert isinstance(server_data, dict)
5967
return self._obj_cls(self, server_data)

gitlab/v4/objects/ldap.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any, List, Union
2+
13
from gitlab import exceptions as exc
24
from gitlab.base import RESTManager, RESTObject, RESTObjectList
35

@@ -17,7 +19,7 @@ class LDAPGroupManager(RESTManager):
1719
_list_filters = ("search", "provider")
1820

1921
@exc.on_http_error(exc.GitlabListError)
20-
def list(self, **kwargs):
22+
def list(self, **kwargs: Any) -> Union[List[LDAPGroup], RESTObjectList]:
2123
"""Retrieve a list of objects.
2224
2325
Args:

0 commit comments

Comments
 (0)