65
65
# Standard Library
66
66
from contextlib import contextmanager
67
67
from functools import wraps
68
- from typing import Optional , Union
68
+ from typing import Optional , Union , TYPE_CHECKING , Callable , Generator
69
69
70
70
# pygit2
71
71
from ._pygit2 import Oid , DiffFile
72
72
from .enums import CheckoutNotify , CheckoutStrategy , CredentialType , StashApplyProgress
73
73
from .errors import check_error , Passthrough
74
74
from .ffi import ffi , C
75
75
from .utils import maybe_string , to_bytes , ptr_to_bytes , StrArray
76
+ from .credentials import Username , UserPass , Keypair
76
77
78
+ _Credentials = Username | UserPass | Keypair
77
79
80
+ if TYPE_CHECKING :
81
+ from .remotes import TransferProgress
82
+ from ._pygit2 import ProxyOpts , PushOptions
78
83
#
79
84
# The payload is the way to pass information from the pygit2 API, through
80
85
# libgit2, to the Python callbacks. And back.
81
86
#
82
87
83
88
84
89
class Payload :
85
- def __init__ (self , ** kw : object ):
90
+ def __init__ (self , ** kw : object ) -> None :
86
91
for key , value in kw .items ():
87
92
setattr (self , key , value )
88
93
self ._stored_exception = None
@@ -113,12 +118,18 @@ class RemoteCallbacks(Payload):
113
118
RemoteCallbacks(certificate=certificate).
114
119
"""
115
120
116
- def __init__ (self , credentials = None , certificate_check = None ):
121
+ push_options : 'PushOptions'
122
+
123
+ def __init__ (
124
+ self ,
125
+ credentials : _Credentials | None = None ,
126
+ certificate_check : Callable [[None , bool , bytes ], bool ] | None = None ,
127
+ ) -> None :
117
128
super ().__init__ ()
118
129
if credentials is not None :
119
- self .credentials = credentials
130
+ self .credentials = credentials # type: ignore[method-assign, assignment]
120
131
if certificate_check is not None :
121
- self .certificate_check = certificate_check
132
+ self .certificate_check = certificate_check # type: ignore[method-assign, assignment]
122
133
123
134
def sideband_progress (self , string : str ) -> None :
124
135
"""
@@ -136,7 +147,7 @@ def credentials(
136
147
url : str ,
137
148
username_from_url : Union [str , None ],
138
149
allowed_types : CredentialType ,
139
- ):
150
+ ) -> _Credentials :
140
151
"""
141
152
Credentials callback. If the remote server requires authentication,
142
153
this function will be called and its return value used for
@@ -159,7 +170,7 @@ def credentials(
159
170
"""
160
171
raise Passthrough
161
172
162
- def certificate_check (self , certificate : None , valid : bool , host : str ) -> bool :
173
+ def certificate_check (self , certificate : None , valid : bool , host : bytes ) -> bool :
163
174
"""
164
175
Certificate callback. Override with your own function to determine
165
176
whether to accept the server's certificate.
@@ -181,7 +192,7 @@ def certificate_check(self, certificate: None, valid: bool, host: str) -> bool:
181
192
182
193
raise Passthrough
183
194
184
- def transfer_progress (self , stats ) :
195
+ def transfer_progress (self , stats : 'TransferProgress' ) -> None :
185
196
"""
186
197
During the download of new data, this will be regularly called with
187
198
the indexer's progress.
@@ -196,7 +207,7 @@ def transfer_progress(self, stats):
196
207
197
208
def push_transfer_progress (
198
209
self , objects_pushed : int , total_objects : int , bytes_pushed : int
199
- ):
210
+ ) -> None :
200
211
"""
201
212
During the upload portion of a push, this will be regularly called
202
213
with progress information.
@@ -207,7 +218,7 @@ def push_transfer_progress(
207
218
Override with your own function to report push transfer progress.
208
219
"""
209
220
210
- def update_tips (self , refname , old , new ) :
221
+ def update_tips (self , refname : str , old : Oid , new : Oid ) -> None :
211
222
"""
212
223
Update tips callback. Override with your own function to report
213
224
reference updates.
@@ -224,7 +235,7 @@ def update_tips(self, refname, old, new):
224
235
The reference's new value.
225
236
"""
226
237
227
- def push_update_reference (self , refname , message ) :
238
+ def push_update_reference (self , refname : str , message : str ) -> None :
228
239
"""
229
240
Push update reference callback. Override with your own function to
230
241
report the remote's acceptance or rejection of reference updates.
@@ -244,7 +255,7 @@ class CheckoutCallbacks(Payload):
244
255
in your class, which you can then pass to checkout operations.
245
256
"""
246
257
247
- def __init__ (self ):
258
+ def __init__ (self ) -> None :
248
259
super ().__init__ ()
249
260
250
261
def checkout_notify_flags (self ) -> CheckoutNotify :
@@ -275,7 +286,7 @@ def checkout_notify(
275
286
baseline : Optional [DiffFile ],
276
287
target : Optional [DiffFile ],
277
288
workdir : Optional [DiffFile ],
278
- ):
289
+ ) -> None :
279
290
"""
280
291
Checkout will invoke an optional notification callback for
281
292
certain cases - you pick which ones via `checkout_notify_flags`.
@@ -290,7 +301,9 @@ def checkout_notify(
290
301
"""
291
302
pass
292
303
293
- def checkout_progress (self , path : str , completed_steps : int , total_steps : int ):
304
+ def checkout_progress (
305
+ self , path : str , completed_steps : int , total_steps : int
306
+ ) -> None :
294
307
"""
295
308
Optional callback to notify the consumer of checkout progress.
296
309
"""
@@ -304,7 +317,7 @@ class StashApplyCallbacks(CheckoutCallbacks):
304
317
in your class, which you can then pass to stash apply or pop operations.
305
318
"""
306
319
307
- def stash_apply_progress (self , progress : StashApplyProgress ):
320
+ def stash_apply_progress (self , progress : StashApplyProgress ) -> None :
308
321
"""
309
322
Stash application progress notification function.
310
323
@@ -373,9 +386,9 @@ def git_fetch_options(payload, opts=None):
373
386
@contextmanager
374
387
def git_proxy_options (
375
388
payload : object ,
376
- opts : object | None = None ,
389
+ opts : Optional [ 'ProxyOpts' ] = None ,
377
390
proxy : None | bool | str = None ,
378
- ):
391
+ ) -> Generator [ 'ProxyOpts' , None , None ] :
379
392
if opts is None :
380
393
opts = ffi .new ('git_proxy_options *' )
381
394
C .git_proxy_options_init (opts , C .GIT_PROXY_OPTIONS_VERSION )
@@ -386,8 +399,8 @@ def git_proxy_options(
386
399
elif type (proxy ) is str :
387
400
opts .type = C .GIT_PROXY_SPECIFIED
388
401
# Keep url in memory, otherwise memory is freed and bad things happen
389
- payload .__proxy_url = ffi .new ('char[]' , to_bytes (proxy ))
390
- opts .url = payload .__proxy_url
402
+ payload .__proxy_url = ffi .new ('char[]' , to_bytes (proxy )) # type: ignore[attr-defined, no-untyped-call]
403
+ opts .url = payload .__proxy_url # type: ignore[attr-defined]
391
404
else :
392
405
raise TypeError ('Proxy must be None, True, or a string' )
393
406
yield opts
0 commit comments