33
44Currently this module only contains repository-related methods for projects.
55"""
6+ from typing import Any , Callable , Dict , List , Optional , TYPE_CHECKING , Union
67
8+ import requests
9+
10+ import gitlab
711from gitlab import cli
812from gitlab import exceptions as exc
913from gitlab import utils
1014
15+ if TYPE_CHECKING :
16+ # When running mypy we use these as the base classes
17+ _RestManagerBase = gitlab .base .RESTManager
18+ _RestObjectBase = gitlab .base .RESTObject
19+ else :
20+ _RestManagerBase = object
21+ _RestObjectBase = object
22+
1123
12- class RepositoryMixin :
24+ class RepositoryMixin ( _RestObjectBase ) :
1325 @cli .register_custom_action ("Project" , ("submodule" , "branch" , "commit_sha" ))
1426 @exc .on_http_error (exc .GitlabUpdateError )
15- def update_submodule (self , submodule , branch , commit_sha , ** kwargs ):
27+ def update_submodule (
28+ self , submodule : str , branch : str , commit_sha : str , ** kwargs : Any
29+ ) -> Union [Dict [str , Any ], requests .Response ]:
1630 """Update a project submodule
1731
1832 Args:
1933 submodule (str): Full path to the submodule
2034 branch (str): Name of the branch to commit into
2135 commit_sha (str): Full commit SHA to update the submodule to
22- commit_message (str): Commit message. If no message is provided, a default one will be set (optional)
36+ commit_message (str): Commit message. If no message is provided, a
37+ default one will be set (optional)
2338
2439 Raises:
2540 GitlabAuthenticationError: If authentication is not correct
@@ -35,7 +50,9 @@ def update_submodule(self, submodule, branch, commit_sha, **kwargs):
3550
3651 @cli .register_custom_action ("Project" , tuple (), ("path" , "ref" , "recursive" ))
3752 @exc .on_http_error (exc .GitlabGetError )
38- def repository_tree (self , path = "" , ref = "" , recursive = False , ** kwargs ):
53+ def repository_tree (
54+ self , path : str = "" , ref : str = "" , recursive : bool = False , ** kwargs : Any
55+ ) -> Union [gitlab .client .GitlabList , List [Dict [str , Any ]]]:
3956 """Return a list of files in the repository.
4057
4158 Args:
@@ -57,7 +74,7 @@ def repository_tree(self, path="", ref="", recursive=False, **kwargs):
5774 list: The representation of the tree
5875 """
5976 gl_path = f"/projects/{ self .get_id ()} /repository/tree"
60- query_data = {"recursive" : recursive }
77+ query_data : Dict [ str , Any ] = {"recursive" : recursive }
6178 if path :
6279 query_data ["path" ] = path
6380 if ref :
@@ -66,7 +83,9 @@ def repository_tree(self, path="", ref="", recursive=False, **kwargs):
6683
6784 @cli .register_custom_action ("Project" , ("sha" ,))
6885 @exc .on_http_error (exc .GitlabGetError )
69- def repository_blob (self , sha , ** kwargs ):
86+ def repository_blob (
87+ self , sha : str , ** kwargs : Any
88+ ) -> Union [Dict [str , Any ], requests .Response ]:
7089 """Return a file by blob SHA.
7190
7291 Args:
@@ -87,8 +106,13 @@ def repository_blob(self, sha, **kwargs):
87106 @cli .register_custom_action ("Project" , ("sha" ,))
88107 @exc .on_http_error (exc .GitlabGetError )
89108 def repository_raw_blob (
90- self , sha , streamed = False , action = None , chunk_size = 1024 , ** kwargs
91- ):
109+ self ,
110+ sha : str ,
111+ streamed : bool = False ,
112+ action : Optional [Callable [..., Any ]] = None ,
113+ chunk_size : int = 1024 ,
114+ ** kwargs : Any ,
115+ ) -> Optional [bytes ]:
92116 """Return the raw file contents for a blob.
93117
94118 Args:
@@ -112,11 +136,15 @@ def repository_raw_blob(
112136 result = self .manager .gitlab .http_get (
113137 path , streamed = streamed , raw = True , ** kwargs
114138 )
139+ if TYPE_CHECKING :
140+ assert isinstance (result , requests .Response )
115141 return utils .response_content (result , streamed , action , chunk_size )
116142
117143 @cli .register_custom_action ("Project" , ("from_" , "to" ))
118144 @exc .on_http_error (exc .GitlabGetError )
119- def repository_compare (self , from_ , to , ** kwargs ):
145+ def repository_compare (
146+ self , from_ : str , to : str , ** kwargs : Any
147+ ) -> Union [Dict [str , Any ], requests .Response ]:
120148 """Return a diff between two branches/commits.
121149
122150 Args:
@@ -137,7 +165,9 @@ def repository_compare(self, from_, to, **kwargs):
137165
138166 @cli .register_custom_action ("Project" )
139167 @exc .on_http_error (exc .GitlabGetError )
140- def repository_contributors (self , ** kwargs ):
168+ def repository_contributors (
169+ self , ** kwargs : Any
170+ ) -> Union [gitlab .client .GitlabList , List [Dict [str , Any ]]]:
141171 """Return a list of contributors for the project.
142172
143173 Args:
@@ -161,8 +191,13 @@ def repository_contributors(self, **kwargs):
161191 @cli .register_custom_action ("Project" , tuple (), ("sha" ,))
162192 @exc .on_http_error (exc .GitlabListError )
163193 def repository_archive (
164- self , sha = None , streamed = False , action = None , chunk_size = 1024 , ** kwargs
165- ):
194+ self ,
195+ sha : str = None ,
196+ streamed : bool = False ,
197+ action : Optional [Callable [..., Any ]] = None ,
198+ chunk_size : int = 1024 ,
199+ ** kwargs : Any ,
200+ ) -> Optional [bytes ]:
166201 """Return a tarball of the repository.
167202
168203 Args:
@@ -189,11 +224,13 @@ def repository_archive(
189224 result = self .manager .gitlab .http_get (
190225 path , query_data = query_data , raw = True , streamed = streamed , ** kwargs
191226 )
227+ if TYPE_CHECKING :
228+ assert isinstance (result , requests .Response )
192229 return utils .response_content (result , streamed , action , chunk_size )
193230
194231 @cli .register_custom_action ("Project" )
195232 @exc .on_http_error (exc .GitlabDeleteError )
196- def delete_merged_branches (self , ** kwargs ) :
233+ def delete_merged_branches (self , ** kwargs : Any ) -> None :
197234 """Delete merged branches.
198235
199236 Args:
0 commit comments