Skip to content

chore: add type-hints to gitlab/v4/objects/groups.py #1668

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
Nov 7, 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
55 changes: 44 additions & 11 deletions gitlab/v4/objects/groups.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from typing import Any, BinaryIO, cast, Dict, List, Optional, Type, TYPE_CHECKING, Union

import requests

import gitlab
from gitlab import cli
from gitlab import exceptions as exc
from gitlab import types
Expand Down Expand Up @@ -74,7 +79,7 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject):

@cli.register_custom_action("Group", ("project_id",))
@exc.on_http_error(exc.GitlabTransferProjectError)
def transfer_project(self, project_id, **kwargs):
def transfer_project(self, project_id: int, **kwargs: Any) -> None:
"""Transfer a project to this group.

Args:
Expand All @@ -90,7 +95,9 @@ def transfer_project(self, project_id, **kwargs):

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

Args:
Expand All @@ -111,7 +118,9 @@ def search(self, scope, search, **kwargs):

@cli.register_custom_action("Group", ("cn", "group_access", "provider"))
@exc.on_http_error(exc.GitlabCreateError)
def add_ldap_group_link(self, cn, group_access, provider, **kwargs):
def add_ldap_group_link(
self, cn: str, group_access: int, provider: str, **kwargs: Any
) -> None:
"""Add an LDAP group link.

Args:
Expand All @@ -131,7 +140,9 @@ def add_ldap_group_link(self, cn, group_access, provider, **kwargs):

@cli.register_custom_action("Group", ("cn",), ("provider",))
@exc.on_http_error(exc.GitlabDeleteError)
def delete_ldap_group_link(self, cn, provider=None, **kwargs):
def delete_ldap_group_link(
self, cn: str, provider: Optional[str] = None, **kwargs: Any
) -> None:
"""Delete an LDAP group link.

Args:
Expand All @@ -151,7 +162,7 @@ def delete_ldap_group_link(self, cn, provider=None, **kwargs):

@cli.register_custom_action("Group")
@exc.on_http_error(exc.GitlabCreateError)
def ldap_sync(self, **kwargs):
def ldap_sync(self, **kwargs: Any) -> None:
"""Sync LDAP groups.

Args:
Expand All @@ -166,7 +177,13 @@ def ldap_sync(self, **kwargs):

@cli.register_custom_action("Group", ("group_id", "group_access"), ("expires_at",))
@exc.on_http_error(exc.GitlabCreateError)
def share(self, group_id, group_access, expires_at=None, **kwargs):
def share(
self,
group_id: int,
group_access: int,
expires_at: Optional[str] = None,
**kwargs: Any,
) -> None:
"""Share the group with a group.

Args:
Expand All @@ -177,18 +194,24 @@ def share(self, group_id, group_access, expires_at=None, **kwargs):
Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabCreateError: If the server failed to perform the request

Returns:
Group
"""
path = f"/groups/{self.get_id()}/share"
data = {
"group_id": group_id,
"group_access": group_access,
"expires_at": expires_at,
}
self.manager.gitlab.http_post(path, post_data=data, **kwargs)
server_data = self.manager.gitlab.http_post(path, post_data=data, **kwargs)
if TYPE_CHECKING:
assert isinstance(server_data, dict)
self._update_attrs(server_data)

@cli.register_custom_action("Group", ("group_id",))
@exc.on_http_error(exc.GitlabDeleteError)
def unshare(self, group_id, **kwargs):
def unshare(self, group_id: int, **kwargs: Any) -> None:
"""Delete a shared group link within a group.

Args:
Expand Down Expand Up @@ -269,8 +292,18 @@ class GroupManager(CRUDMixin, RESTManager):
)
_types = {"avatar": types.ImageAttribute, "skip_groups": types.ListAttribute}

def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> Group:
return cast(Group, super().get(id=id, lazy=lazy, **kwargs))

@exc.on_http_error(exc.GitlabImportError)
def import_group(self, file, path, name, parent_id=None, **kwargs):
def import_group(
self,
file: BinaryIO,
path: str,
name: str,
parent_id: Optional[str] = None,
**kwargs: Any,
) -> Union[Dict[str, Any], requests.Response]:
"""Import a group from an archive file.

Args:
Expand Down Expand Up @@ -304,7 +337,7 @@ class GroupSubgroup(RESTObject):

class GroupSubgroupManager(ListMixin, RESTManager):
_path = "/groups/%(group_id)s/subgroups"
_obj_cls = GroupSubgroup
_obj_cls: Union[Type["GroupDescendantGroup"], Type[GroupSubgroup]] = GroupSubgroup
_from_parent_attrs = {"group_id": "id"}
_list_filters = (
"skip_groups",
Expand All @@ -331,4 +364,4 @@ class GroupDescendantGroupManager(GroupSubgroupManager):
"""

_path = "/groups/%(group_id)s/descendant_groups"
_obj_cls = GroupDescendantGroup
_obj_cls: Type[GroupDescendantGroup] = GroupDescendantGroup
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ ignore_errors = true

[[tool.mypy.overrides]] # Overrides to negate above patterns
module = [
"gitlab.v4.objects.groups",
"gitlab.v4.objects.merge_requests",
"gitlab.v4.objects.projects",
"gitlab.v4.objects.users"
"gitlab.v4.objects.users",
]
ignore_errors = false

Expand Down