Skip to content

Commit 3727cbd

Browse files
chore: add type hints to gitlab/base.py
1 parent fdec039 commit 3727cbd

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

gitlab/base.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

1818
import importlib
19+
from typing import Any, Dict, Optional
1920

21+
from .client import Gitlab, GitlabList
2022

2123
__all__ = [
2224
"RESTObject",
@@ -38,7 +40,7 @@ class RESTObject(object):
3840

3941
_id_attr = "id"
4042

41-
def __init__(self, manager, attrs):
43+
def __init__(self, manager: "RESTManager", attrs: Dict[str, Any]) -> None:
4244
self.__dict__.update(
4345
{
4446
"manager": manager,
@@ -50,18 +52,18 @@ def __init__(self, manager, attrs):
5052
self.__dict__["_parent_attrs"] = self.manager.parent_attrs
5153
self._create_managers()
5254

53-
def __getstate__(self):
55+
def __getstate__(self) -> Dict[str, Any]:
5456
state = self.__dict__.copy()
5557
module = state.pop("_module")
5658
state["_module_name"] = module.__name__
5759
return state
5860

59-
def __setstate__(self, state):
61+
def __setstate__(self, state: Dict[str, Any]) -> None:
6062
module_name = state.pop("_module_name")
6163
self.__dict__.update(state)
6264
self.__dict__["_module"] = importlib.import_module(module_name)
6365

64-
def __getattr__(self, name):
66+
def __getattr__(self, name: str) -> Any:
6567
try:
6668
return self.__dict__["_updated_attrs"][name]
6769
except KeyError:
@@ -90,15 +92,15 @@ def __getattr__(self, name):
9092
except KeyError:
9193
raise AttributeError(name)
9294

93-
def __setattr__(self, name, value):
95+
def __setattr__(self, name: str, value) -> None:
9496
self.__dict__["_updated_attrs"][name] = value
9597

96-
def __str__(self):
98+
def __str__(self) -> str:
9799
data = self._attrs.copy()
98100
data.update(self._updated_attrs)
99101
return "%s => %s" % (type(self), data)
100102

101-
def __repr__(self):
103+
def __repr__(self) -> str:
102104
if self._id_attr:
103105
return "<%s %s:%s>" % (
104106
self.__class__.__name__,
@@ -108,25 +110,25 @@ def __repr__(self):
108110
else:
109111
return "<%s>" % self.__class__.__name__
110112

111-
def __eq__(self, other):
113+
def __eq__(self, other) -> bool:
112114
if self.get_id() and other.get_id():
113115
return self.get_id() == other.get_id()
114116
return super(RESTObject, self) == other
115117

116-
def __ne__(self, other):
118+
def __ne__(self, other) -> bool:
117119
if self.get_id() and other.get_id():
118120
return self.get_id() != other.get_id()
119121
return super(RESTObject, self) != other
120122

121123
def __dir__(self):
122124
return super(RESTObject, self).__dir__() + list(self.attributes)
123125

124-
def __hash__(self):
126+
def __hash__(self) -> int:
125127
if not self.get_id():
126128
return super(RESTObject, self).__hash__()
127129
return hash(self.get_id())
128130

129-
def _create_managers(self):
131+
def _create_managers(self) -> None:
130132
managers = getattr(self, "_managers", None)
131133
if managers is None:
132134
return
@@ -136,7 +138,7 @@ def _create_managers(self):
136138
manager = cls(self.manager.gitlab, parent=self)
137139
self.__dict__[attr] = manager
138140

139-
def _update_attrs(self, new_attrs):
141+
def _update_attrs(self, new_attrs) -> None:
140142
self.__dict__["_updated_attrs"] = {}
141143
self.__dict__["_attrs"] = new_attrs
142144

@@ -147,7 +149,7 @@ def get_id(self):
147149
return getattr(self, self._id_attr)
148150

149151
@property
150-
def attributes(self):
152+
def attributes(self) -> Dict[str, Any]:
151153
d = self.__dict__["_updated_attrs"].copy()
152154
d.update(self.__dict__["_attrs"])
153155
d.update(self.__dict__["_parent_attrs"])
@@ -169,7 +171,7 @@ class RESTObjectList(object):
169171
_list: A GitlabList object
170172
"""
171173

172-
def __init__(self, manager, obj_cls, _list):
174+
def __init__(self, manager: "RESTManager", obj_cls, _list: GitlabList) -> None:
173175
"""Creates an objects list from a GitlabList.
174176
175177
You should not create objects of this type, but use managers list()
@@ -184,10 +186,10 @@ def __init__(self, manager, obj_cls, _list):
184186
self._obj_cls = obj_cls
185187
self._list = _list
186188

187-
def __iter__(self):
189+
def __iter__(self) -> "RESTObjectList":
188190
return self
189191

190-
def __len__(self):
192+
def __len__(self) -> int:
191193
return len(self._list)
192194

193195
def __next__(self):
@@ -198,38 +200,38 @@ def next(self):
198200
return self._obj_cls(self.manager, data)
199201

200202
@property
201-
def current_page(self):
203+
def current_page(self) -> int:
202204
"""The current page number."""
203205
return self._list.current_page
204206

205207
@property
206-
def prev_page(self):
208+
def prev_page(self) -> int:
207209
"""The previous page number.
208210
209211
If None, the current page is the first.
210212
"""
211213
return self._list.prev_page
212214

213215
@property
214-
def next_page(self):
216+
def next_page(self) -> int:
215217
"""The next page number.
216218
217219
If None, the current page is the last.
218220
"""
219221
return self._list.next_page
220222

221223
@property
222-
def per_page(self):
224+
def per_page(self) -> int:
223225
"""The number of items per page."""
224226
return self._list.per_page
225227

226228
@property
227-
def total_pages(self):
229+
def total_pages(self) -> int:
228230
"""The total number of pages."""
229231
return self._list.total_pages
230232

231233
@property
232-
def total(self):
234+
def total(self) -> int:
233235
"""The total number of items."""
234236
return self._list.total
235237

@@ -243,10 +245,11 @@ class RESTManager(object):
243245
``_obj_cls``: The class of objects that will be created
244246
"""
245247

246-
_path = None
247-
_obj_cls = None
248+
_path: Optional[str] = None
249+
_obj_cls: Optional[Any] = None
250+
_from_parent_attrs: Dict[str, Any] = {}
248251

249-
def __init__(self, gl, parent=None):
252+
def __init__(self, gl: Gitlab, parent: Optional[RESTObject] = None) -> None:
250253
"""REST manager constructor.
251254
252255
Args:
@@ -259,23 +262,25 @@ def __init__(self, gl, parent=None):
259262
self._computed_path = self._compute_path()
260263

261264
@property
262-
def parent_attrs(self):
265+
def parent_attrs(self) -> Optional[Dict[str, Any]]:
263266
return self._parent_attrs
264267

265-
def _compute_path(self, path=None):
268+
def _compute_path(self, path: Optional[str] = None) -> Optional[str]:
266269
self._parent_attrs = {}
267270
if path is None:
268271
path = self._path
272+
if path is None:
273+
return None
269274
if self._parent is None or not hasattr(self, "_from_parent_attrs"):
270275
return path
271276

272277
data = {
273278
self_attr: getattr(self._parent, parent_attr, None)
274-
for self_attr, parent_attr in self._from_parent_attrs.items()
279+
for self_attr, parent_attr in self._from_parent_attrs.items() # type: ignore
275280
}
276281
self._parent_attrs = data
277282
return path % data
278283

279284
@property
280-
def path(self):
285+
def path(self) -> Optional[str]:
281286
return self._computed_path

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy