Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3727cbd

Browse files
chore: add type hints to gitlab/base.py
1 parentfdec039 commit3727cbd

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
importimportlib
19+
fromtypingimportAny,Dict,Optional
1920

21+
from .clientimportGitlab,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
returnstate
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
returnself.__dict__["_updated_attrs"][name]
6769
exceptKeyError:
@@ -90,15 +92,15 @@ def __getattr__(self, name):
9092
exceptKeyError:
9193
raiseAttributeError(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
ifself._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
ifself.get_id()andother.get_id():
113115
returnself.get_id()==other.get_id()
114116
returnsuper(RESTObject,self)==other
115117

116-
def__ne__(self,other):
118+
def__ne__(self,other)->bool:
117119
ifself.get_id()andother.get_id():
118120
returnself.get_id()!=other.get_id()
119121
returnsuper(RESTObject,self)!=other
120122

121123
def__dir__(self):
122124
returnsuper(RESTObject,self).__dir__()+list(self.attributes)
123125

124-
def__hash__(self):
126+
def__hash__(self)->int:
125127
ifnotself.get_id():
126128
returnsuper(RESTObject,self).__hash__()
127129
returnhash(self.get_id())
128130

129-
def_create_managers(self):
131+
def_create_managers(self)->None:
130132
managers=getattr(self,"_managers",None)
131133
ifmanagersisNone:
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
returngetattr(self,self._id_attr)
148150

149151
@property
150-
defattributes(self):
152+
defattributes(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
returnself
189191

190-
def__len__(self):
192+
def__len__(self)->int:
191193
returnlen(self._list)
192194

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

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

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

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

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

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

231233
@property
232-
deftotal(self):
234+
deftotal(self)->int:
233235
"""The total number of items."""
234236
returnself._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-
defparent_attrs(self):
265+
defparent_attrs(self)->Optional[Dict[str,Any]]:
263266
returnself._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
ifpathisNone:
268271
path=self._path
272+
ifpathisNone:
273+
returnNone
269274
ifself._parentisNoneornothasattr(self,"_from_parent_attrs"):
270275
returnpath
271276

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

279284
@property
280-
defpath(self):
285+
defpath(self)->Optional[str]:
281286
returnself._computed_path

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp