Skip to content

chore: add type-hints to gitlab/v4/objects/users.py #1515

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
Oct 6, 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
23 changes: 15 additions & 8 deletions gitlab/v4/objects/users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from typing import Any, cast, Dict, List, Union

import requests

from gitlab import cli
from gitlab import exceptions as exc
from gitlab import types
from gitlab.base import RequiredOptional, RESTManager, RESTObject
from gitlab.base import RequiredOptional, RESTManager, RESTObject, RESTObjectList
from gitlab.mixins import (
CreateMixin,
CRUDMixin,
Expand Down Expand Up @@ -129,7 +133,7 @@ class User(SaveMixin, ObjectDeleteMixin, RESTObject):

@cli.register_custom_action("User")
@exc.on_http_error(exc.GitlabBlockError)
def block(self, **kwargs):
def block(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Block the user.

Args:
Expand All @@ -150,7 +154,7 @@ def block(self, **kwargs):

@cli.register_custom_action("User")
@exc.on_http_error(exc.GitlabFollowError)
def follow(self, **kwargs):
def follow(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Follow the user.

Args:
Expand All @@ -168,7 +172,7 @@ def follow(self, **kwargs):

@cli.register_custom_action("User")
@exc.on_http_error(exc.GitlabUnfollowError)
def unfollow(self, **kwargs):
def unfollow(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Unfollow the user.

Args:
Expand All @@ -186,7 +190,7 @@ def unfollow(self, **kwargs):

@cli.register_custom_action("User")
@exc.on_http_error(exc.GitlabUnblockError)
def unblock(self, **kwargs):
def unblock(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Unblock the user.

Args:
Expand All @@ -207,7 +211,7 @@ def unblock(self, **kwargs):

@cli.register_custom_action("User")
@exc.on_http_error(exc.GitlabDeactivateError)
def deactivate(self, **kwargs):
def deactivate(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Deactivate the user.

Args:
Expand All @@ -228,7 +232,7 @@ def deactivate(self, **kwargs):

@cli.register_custom_action("User")
@exc.on_http_error(exc.GitlabActivateError)
def activate(self, **kwargs):
def activate(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Activate the user.

Args:
Expand Down Expand Up @@ -319,6 +323,9 @@ class UserManager(CRUDMixin, RESTManager):
)
_types = {"confirm": types.LowercaseStringAttribute, "avatar": types.ImageAttribute}

def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> User:
return cast(User, super().get(id=id, lazy=lazy, **kwargs))
Comment on lines +326 to +327
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: I already merged this for projects but now it has me thinking, is there any way to avoid adding all this boilerplate for typing? There will be a lot of these I imagine if as we go module by module, and I guess will need to be copy/pasted for a lot of mixin usage.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried and tried again and tried again... But so far I can't figure out a way to do it. If you can figure it out it will be awesome!

There are ways to do it if you return a self object. But since we are returning self._obj_cls() it doesn't seem like things are smart enough to understand that yet. Well at least not mypy

This will help editors and the type-checker in knowing that gl.users.get() returns a User object. Otherwise it will think it returns a RESTObject which isn't as useful.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you will probably like my idea for handling gl.users.list() even less 😜

At the moment my best solution for that seems to be adding a UserList class or something like that. I haven't figured it out yet but thinking about it.



class ProjectUser(RESTObject):
pass
Expand Down Expand Up @@ -470,7 +477,7 @@ class UserProjectManager(ListMixin, CreateMixin, RESTManager):
"id_before",
)

def list(self, **kwargs):
def list(self, **kwargs: Any) -> Union[RESTObjectList, List[RESTObject]]:
"""Retrieve a list of objects.

Args:
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ ignore_errors = true

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

Expand Down