Description
Currently the all of RESTObject attributes are typed as Any
by the __getattr__
method. This means there is no type hinting of any REST API objects such as Project:
from gitlab.v4.objects import Project
from typing import reveal_type
def test(proj: Project) -> None:
reveal_type(proj.description)
$ mypy --follow-imports=silent typing_objects.py
typing_objects.py:6: note: Revealed type is "Any"
This also means accessing a non-existent attribute will also pass type checking:
from gitlab.v4.objects import Project
from typing import reveal_type
def test(proj: Project) -> None:
reveal_type(proj.foobar_does_not_exist)
$ mypy --follow-imports=silent typing_objects.py
typing_objects.py:6: note: Revealed type is "Any"
One solution can be adding extra type annotation to the RESTObject subclass body:
class Project(
RefreshMixin, SaveMixin, ObjectDeleteMixin, RepositoryMixin, UploadMixin, RESTObject
):
description: str
typing_objects.py:6: note: Revealed type is "builtins.str"
This does not fix the non-existent attribute issue but in a lot cases using Any will raise warning by type checker. (for example, returning Any)
Another question is having a source of information on the available attributes. The official documentation only provides examples of the JSON objects but does not document attributes in detail. There is an OpenAPI schema which can be used.