Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Type hinting RESTObject attributes #3086

Open
@igorp-collabora

Description

@igorp-collabora

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions