Skip to content

RepositoryMixin base class unresolvable by pyright due to implicit submodule access #3342

@janfrederik

Description

@janfrederik

Description

In gitlab/v4/objects/repositories.py, the RepositoryMixin class uses a conditional base class for type checking:

import gitlab

if TYPE_CHECKING:
    _RestObjectBase = gitlab.base.RESTObject
else:
    _RestObjectBase = object

class RepositoryMixin(_RestObjectBase):
    ...

Pyright (and basedpyright) cannot resolve gitlab.base.RESTObject here because import gitlab does not make gitlab.base accessible as an attribute — Python only guarantees submodule access after an explicit import of that submodule. This is a runtime side effect of the import system that type checkers (correctly) don't model. See pyright docs on implicit submodule access.

As a result, _RestObjectBase is Unknown, which propagates through RepositoryMixin and into all classes that inherit it (e.g. Project). This causes spurious reportUnknownMemberType / reportUnknownVariableType warnings for any code using Project attributes.

Suggested fix

Add an explicit import gitlab.base inside the TYPE_CHECKING block:

if TYPE_CHECKING:
    import gitlab.base
    _RestObjectBase = gitlab.base.RESTObject
else:
    _RestObjectBase = object

This is a one-line change with no runtime impact (it's only evaluated by type checkers).

The same pattern may exist in other files — a quick search for gitlab.base.RESTObject or gitlab.base.RESTManager in TYPE_CHECKING blocks would surface them.

Context

  • Discovered via basedpyright#1719
  • Affects pyright as well when reportUnknownMemberType / reportUnknownVariableType are enabled

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions