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

Commit7ba5995

Browse files
authored
Merge pull request#1695 from python-gitlab/jlvillal/mypy_epics
chore: add type-hints to remaining gitlab/v4/objects/*.py files
2 parentsa390ec3 +0c22bd9 commit7ba5995

File tree

13 files changed

+353
-90
lines changed

13 files changed

+353
-90
lines changed

‎gitlab/base.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ def _create_managers(self) -> None:
150150
# annotations. If an attribute is annotated as being a *Manager type
151151
# then we create the manager and assign it to the attribute.
152152
forattr,annotationinsorted(self.__annotations__.items()):
153+
# We ignore creating a manager for the 'manager' attribute as that
154+
# is done in the self.__init__() method
155+
ifattrin ("manager",):
156+
continue
153157
ifnotisinstance(annotation, (type,str)):
154158
continue
155159
ifisinstance(annotation,type):

‎gitlab/v4/objects/epics.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
fromtypingimportAny,cast,Dict,Optional,TYPE_CHECKING,Union
2+
13
fromgitlabimportexceptionsasexc
24
fromgitlabimporttypes
35
fromgitlab.baseimportRequiredOptional,RESTManager,RESTObject
@@ -42,11 +44,17 @@ class GroupEpicManager(CRUDMixin, RESTManager):
4244
)
4345
_types= {"labels":types.ListAttribute}
4446

47+
defget(self,id:Union[str,int],lazy:bool=False,**kwargs:Any)->GroupEpic:
48+
returncast(GroupEpic,super().get(id=id,lazy=lazy,**kwargs))
49+
4550

4651
classGroupEpicIssue(ObjectDeleteMixin,SaveMixin,RESTObject):
4752
_id_attr="epic_issue_id"
53+
# Define type for 'manager' here So mypy won't complain about
54+
# 'self.manager.update()' call in the 'save' method.
55+
manager:"GroupEpicIssueManager"
4856

49-
defsave(self,**kwargs):
57+
defsave(self,**kwargs:Any)->None:
5058
"""Save the changes made to the object to the server.
5159
5260
The object is updated to match what the server returns.
@@ -78,7 +86,9 @@ class GroupEpicIssueManager(
7886
_update_attrs=RequiredOptional(optional=("move_before_id","move_after_id"))
7987

8088
@exc.on_http_error(exc.GitlabCreateError)
81-
defcreate(self,data,**kwargs):
89+
defcreate(
90+
self,data:Optional[Dict[str,Any]]=None,**kwargs:Any
91+
)->GroupEpicIssue:
8292
"""Create a new object.
8393
8494
Args:
@@ -94,9 +104,13 @@ def create(self, data, **kwargs):
94104
RESTObject: A new instance of the manage object class build with
95105
the data sent by the server
96106
"""
107+
ifTYPE_CHECKING:
108+
assertdataisnotNone
97109
CreateMixin._check_missing_create_attrs(self,data)
98110
path=f"{self.path}/{data.pop('issue_id')}"
99111
server_data=self.gitlab.http_post(path,**kwargs)
112+
ifTYPE_CHECKING:
113+
assertisinstance(server_data,dict)
100114
# The epic_issue_id attribute doesn't exist when creating the resource,
101115
# but is used everywhere elese. Let's create it to be consistent client
102116
# side

‎gitlab/v4/objects/files.py‎

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
importbase64
2+
fromtypingimportAny,Callable,cast,Dict,List,Optional,TYPE_CHECKING
3+
4+
importrequests
25

36
fromgitlabimportcli
47
fromgitlabimportexceptionsasexc
@@ -22,6 +25,8 @@
2225
classProjectFile(SaveMixin,ObjectDeleteMixin,RESTObject):
2326
_id_attr="file_path"
2427
_short_print_attr="file_path"
28+
file_path:str
29+
manager:"ProjectFileManager"
2530

2631
defdecode(self)->bytes:
2732
"""Returns the decoded content of the file.
@@ -31,7 +36,11 @@ def decode(self) -> bytes:
3136
"""
3237
returnbase64.b64decode(self.content)
3338

34-
defsave(self,branch,commit_message,**kwargs):
39+
# NOTE(jlvillal): Signature doesn't match SaveMixin.save() so ignore
40+
# type error
41+
defsave(# type: ignore
42+
self,branch:str,commit_message:str,**kwargs:Any
43+
)->None:
3544
"""Save the changes made to the file to the server.
3645
3746
The object is updated to match what the server returns.
@@ -50,7 +59,12 @@ def save(self, branch, commit_message, **kwargs):
5059
self.file_path=self.file_path.replace("/","%2F")
5160
super(ProjectFile,self).save(**kwargs)
5261

53-
defdelete(self,branch,commit_message,**kwargs):
62+
@exc.on_http_error(exc.GitlabDeleteError)
63+
# NOTE(jlvillal): Signature doesn't match DeleteMixin.delete() so ignore
64+
# type error
65+
defdelete(# type: ignore
66+
self,branch:str,commit_message:str,**kwargs:Any
67+
)->None:
5468
"""Delete the file from the server.
5569
5670
Args:
@@ -80,7 +94,11 @@ class ProjectFileManager(GetMixin, CreateMixin, UpdateMixin, DeleteMixin, RESTMa
8094
)
8195

8296
@cli.register_custom_action("ProjectFileManager", ("file_path","ref"))
83-
defget(self,file_path,ref,**kwargs):
97+
# NOTE(jlvillal): Signature doesn't match UpdateMixin.update() so ignore
98+
# type error
99+
defget(# type: ignore
100+
self,file_path:str,ref:str,**kwargs:Any
101+
)->ProjectFile:
84102
"""Retrieve a single file.
85103
86104
Args:
@@ -95,15 +113,17 @@ def get(self, file_path, ref, **kwargs):
95113
Returns:
96114
object: The generated RESTObject
97115
"""
98-
returnGetMixin.get(self,file_path,ref=ref,**kwargs)
116+
returncast(ProjectFile,GetMixin.get(self,file_path,ref=ref,**kwargs))
99117

100118
@cli.register_custom_action(
101119
"ProjectFileManager",
102120
("file_path","branch","content","commit_message"),
103121
("encoding","author_email","author_name"),
104122
)
105123
@exc.on_http_error(exc.GitlabCreateError)
106-
defcreate(self,data,**kwargs):
124+
defcreate(
125+
self,data:Optional[Dict[str,Any]]=None,**kwargs:Any
126+
)->ProjectFile:
107127
"""Create a new object.
108128
109129
Args:
@@ -120,15 +140,23 @@ def create(self, data, **kwargs):
120140
GitlabCreateError: If the server cannot perform the request
121141
"""
122142

143+
ifTYPE_CHECKING:
144+
assertdataisnotNone
123145
self._check_missing_create_attrs(data)
124146
new_data=data.copy()
125147
file_path=new_data.pop("file_path").replace("/","%2F")
126148
path=f"{self.path}/{file_path}"
127149
server_data=self.gitlab.http_post(path,post_data=new_data,**kwargs)
150+
ifTYPE_CHECKING:
151+
assertisinstance(server_data,dict)
128152
returnself._obj_cls(self,server_data)
129153

130154
@exc.on_http_error(exc.GitlabUpdateError)
131-
defupdate(self,file_path,new_data=None,**kwargs):
155+
# NOTE(jlvillal): Signature doesn't match UpdateMixin.update() so ignore
156+
# type error
157+
defupdate(# type: ignore
158+
self,file_path:str,new_data:Optional[Dict[str,Any]]=None,**kwargs:Any
159+
)->Dict[str,Any]:
132160
"""Update an object on the server.
133161
134162
Args:
@@ -149,13 +177,20 @@ def update(self, file_path, new_data=None, **kwargs):
149177
data["file_path"]=file_path
150178
path=f"{self.path}/{file_path}"
151179
self._check_missing_update_attrs(data)
152-
returnself.gitlab.http_put(path,post_data=data,**kwargs)
180+
result=self.gitlab.http_put(path,post_data=data,**kwargs)
181+
ifTYPE_CHECKING:
182+
assertisinstance(result,dict)
183+
returnresult
153184

154185
@cli.register_custom_action(
155186
"ProjectFileManager", ("file_path","branch","commit_message")
156187
)
157188
@exc.on_http_error(exc.GitlabDeleteError)
158-
defdelete(self,file_path,branch,commit_message,**kwargs):
189+
# NOTE(jlvillal): Signature doesn't match DeleteMixin.delete() so ignore
190+
# type error
191+
defdelete(# type: ignore
192+
self,file_path:str,branch:str,commit_message:str,**kwargs:Any
193+
)->None:
159194
"""Delete a file on the server.
160195
161196
Args:
@@ -175,8 +210,14 @@ def delete(self, file_path, branch, commit_message, **kwargs):
175210
@cli.register_custom_action("ProjectFileManager", ("file_path","ref"))
176211
@exc.on_http_error(exc.GitlabGetError)
177212
defraw(
178-
self,file_path,ref,streamed=False,action=None,chunk_size=1024,**kwargs
179-
):
213+
self,
214+
file_path:str,
215+
ref:str,
216+
streamed:bool=False,
217+
action:Optional[Callable[...,Any]]=None,
218+
chunk_size:int=1024,
219+
**kwargs:Any,
220+
)->Optional[bytes]:
180221
"""Return the content of a file for a commit.
181222
182223
Args:
@@ -203,11 +244,13 @@ def raw(
203244
result=self.gitlab.http_get(
204245
path,query_data=query_data,streamed=streamed,raw=True,**kwargs
205246
)
247+
ifTYPE_CHECKING:
248+
assertisinstance(result,requests.Response)
206249
returnutils.response_content(result,streamed,action,chunk_size)
207250

208251
@cli.register_custom_action("ProjectFileManager", ("file_path","ref"))
209252
@exc.on_http_error(exc.GitlabListError)
210-
defblame(self,file_path,ref,**kwargs):
253+
defblame(self,file_path:str,ref:str,**kwargs:Any)->List[Dict[str,Any]]:
211254
"""Return the content of a file for a commit.
212255
213256
Args:
@@ -225,4 +268,7 @@ def blame(self, file_path, ref, **kwargs):
225268
file_path=file_path.replace("/","%2F").replace(".","%2E")
226269
path=f"{self.path}/{file_path}/blame"
227270
query_data= {"ref":ref}
228-
returnself.gitlab.http_list(path,query_data,**kwargs)
271+
result=self.gitlab.http_list(path,query_data,**kwargs)
272+
ifTYPE_CHECKING:
273+
assertisinstance(result,list)
274+
returnresult

‎gitlab/v4/objects/geo_nodes.py‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
fromtypingimportAny,cast,Dict,List,TYPE_CHECKING,Union
2+
13
fromgitlabimportcli
24
fromgitlabimportexceptionsasexc
35
fromgitlab.baseimportRequiredOptional,RESTManager,RESTObject
@@ -18,7 +20,7 @@
1820
classGeoNode(SaveMixin,ObjectDeleteMixin,RESTObject):
1921
@cli.register_custom_action("GeoNode")
2022
@exc.on_http_error(exc.GitlabRepairError)
21-
defrepair(self,**kwargs):
23+
defrepair(self,**kwargs:Any)->None:
2224
"""Repair the OAuth authentication of the geo node.
2325
2426
Args:
@@ -30,11 +32,13 @@ def repair(self, **kwargs):
3032
"""
3133
path=f"/geo_nodes/{self.get_id()}/repair"
3234
server_data=self.manager.gitlab.http_post(path,**kwargs)
35+
ifTYPE_CHECKING:
36+
assertisinstance(server_data,dict)
3337
self._update_attrs(server_data)
3438

3539
@cli.register_custom_action("GeoNode")
3640
@exc.on_http_error(exc.GitlabGetError)
37-
defstatus(self,**kwargs):
41+
defstatus(self,**kwargs:Any)->Dict[str,Any]:
3842
"""Get the status of the geo node.
3943
4044
Args:
@@ -48,7 +52,10 @@ def status(self, **kwargs):
4852
dict: The status of the geo node
4953
"""
5054
path=f"/geo_nodes/{self.get_id()}/status"
51-
returnself.manager.gitlab.http_get(path,**kwargs)
55+
result=self.manager.gitlab.http_get(path,**kwargs)
56+
ifTYPE_CHECKING:
57+
assertisinstance(result,dict)
58+
returnresult
5259

5360

5461
classGeoNodeManager(RetrieveMixin,UpdateMixin,DeleteMixin,RESTManager):
@@ -58,9 +65,12 @@ class GeoNodeManager(RetrieveMixin, UpdateMixin, DeleteMixin, RESTManager):
5865
optional=("enabled","url","files_max_capacity","repos_max_capacity"),
5966
)
6067

68+
defget(self,id:Union[str,int],lazy:bool=False,**kwargs:Any)->GeoNode:
69+
returncast(GeoNode,super().get(id=id,lazy=lazy,**kwargs))
70+
6171
@cli.register_custom_action("GeoNodeManager")
6272
@exc.on_http_error(exc.GitlabGetError)
63-
defstatus(self,**kwargs):
73+
defstatus(self,**kwargs:Any)->List[Dict[str,Any]]:
6474
"""Get the status of all the geo nodes.
6575
6676
Args:
@@ -73,11 +83,14 @@ def status(self, **kwargs):
7383
Returns:
7484
list: The status of all the geo nodes
7585
"""
76-
returnself.gitlab.http_list("/geo_nodes/status",**kwargs)
86+
result=self.gitlab.http_list("/geo_nodes/status",**kwargs)
87+
ifTYPE_CHECKING:
88+
assertisinstance(result,list)
89+
returnresult
7790

7891
@cli.register_custom_action("GeoNodeManager")
7992
@exc.on_http_error(exc.GitlabGetError)
80-
defcurrent_failures(self,**kwargs):
93+
defcurrent_failures(self,**kwargs:Any)->List[Dict[str,Any]]:
8194
"""Get the list of failures on the current geo node.
8295
8396
Args:
@@ -90,4 +103,7 @@ def current_failures(self, **kwargs):
90103
Returns:
91104
list: The list of failures
92105
"""
93-
returnself.gitlab.http_list("/geo_nodes/current/failures",**kwargs)
106+
result=self.gitlab.http_list("/geo_nodes/current/failures",**kwargs)
107+
ifTYPE_CHECKING:
108+
assertisinstance(result,list)
109+
returnresult

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp