Repository navigation
Type hint on ListMixin.list is too strict #2062
Description
Activity
I don't think it is too strict. In fact I think it is too loose. Because if you get a list of users the type-checkers will think it is a
List[RestObject]when in reality it is aList[gitlab.v4.objects.Users]😟 NOTE:gitlab.v4.objects.Usersis derived fromRestObject.So far we have not figured out an easy/elegant way to solve this problem. One idea is to do like we do with the
getmethods and add a wrapper method to every class but theRESTObjectListkind of causes problems there...Reacted by Ellis BreenJohn, I think this could be solved with generic class objects. Would something like this patch do the job?
https://gist.github.com/vectro/fa5a88b320407f400da2ed54aecfa03fReacted by Ellis BreenJohn, I think this could be solved with generic class objects. Would something like this patch do the job? https://gist.github.com/vectro/fa5a88b320407f400da2ed54aecfa03f
I'm not sure.
mypyraised 800 errors when I tried that patch?Basically errors like this:
gitlab/v4/objects/groups.py:254: error: Definition of "_parent" in base class "GetMixin" is incompatible with definition in base class "ListMixin" gitlab/v4/objects/groups.py:254: error: Definition of "_parent" in base class "GetMixin" is incompatible with definition in base class "CreateMixin" gitlab/v4/objects/groups.py:254: error: Definition of "_parent" in base class "GetMixin" is incompatible with definition in base class "UpdateMixin" gitlab/v4/objects/groups.py:254: error: Definition of "_parent" in base class "GetMixin" is incompatible with definition in base class "DeleteMixin" gitlab/v4/objects/groups.py:254: error: Definition of "_parent" in base class "ListMixin" is incompatible with definition in base class "CreateMixin" gitlab/v4/objects/groups.py:254: error: Definition of "_parent" in base class "ListMixin" is incompatible with definition in base class "UpdateMixin" gitlab/v4/objects/groups.py:254: error: Definition of "_parent" in base class "ListMixin" is incompatible with definition in base class "DeleteMixin" gitlab/v4/objects/groups.py:254: error: Definition of "_parent" in base class "CreateMixin" is incompatible with definition in base class "UpdateMixin" gitlab/v4/objects/groups.py:254: error: Definition of "_parent" in base class "CreateMixin" is incompatible with definition in base class "DeleteMixin" gitlab/v4/objects/groups.py:254: error: Definition of "_parent" in base class "UpdateMixin" is incompatible with definition in base class "DeleteMixin"Hmm, that's interesting. I don't see that error on my end. What mypy are you using? I have 0.812 with default settings.
Hmm, that's interesting. I don't see that error on my end. What mypy are you using? I have 0.812 with default settings.
I used:
$ tox -e mypyAnd we are using
mypy==0.960It looks like mypy just doesn't like the redundant type hints on the intermediate abstract classes. If you move these to the base class these errors go away. See here for a sketch: https://gist.github.com/vectro/c6b6dd921ddf7fded5203a97e066e904
Looks like mypy folks are still figuring out what to do about diamond inheritance: python/mypy#1065
It looks like mypy just doesn't like the redundant type hints on the intermediate abstract classes. If you move these to the base class these errors go away. See here for a sketch: https://gist.github.com/vectro/c6b6dd921ddf7fded5203a97e066e904
Looks like mypy folks are still figuring out what to do about diamond inheritance: python/mypy#1065
If you want, maybe the best way forward would be for you to propose a pull request. You can always mark it as "draft" if you like.
That would make it easier to review and see that it passes the checks.
@JohnVillalovos, sure, thanks, done.
@vectro as I said in the PR it's best to follow the versions we track here by using our tox setup :)
- locked as resolved and limited conversation to collaborators
on Feb 9, 2026
The type hint on ListMixin.list says that it returns a
Union[base.RESTObjectList, List[base.RestObject]]but this is too strict; each concrete subclass of ListMixin will actually return a Sequence[self._obj_cls].I'm somewhat new to Python type hints but could this be solved by introducing a type variable and connecting it to both _obj_cls and list()?