Skip to content

Commit fb0bfd7

Browse files
committed
improve types according to test/test_remote.py
1 parent edcccfc commit fb0bfd7

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

pygit2/_libgit2/ffi.pyi

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@ NULL: NULL_TYPE = ...
3535
char = NewType('char', object)
3636
char_pointer = NewType('char_pointer', object)
3737

38+
class size_t:
39+
def __getitem__(self, item: Literal[0]) -> int: ...
40+
3841
class _Pointer(Generic[T]):
3942
def __setitem__(self, item: Literal[0], a: T) -> None: ...
4043
@overload
4144
def __getitem__(self, item: Literal[0]) -> T: ...
4245
@overload
4346
def __getitem__(self, item: slice[None, None, None]) -> bytes: ...
4447

48+
class _MultiPointer(Generic[T]):
49+
def __getitem__(self, item: int) -> T: ...
50+
4551
class ArrayC(Generic[T]):
4652
# incomplete!
4753
# def _len(self, ?) -> ?: ...
@@ -83,6 +89,13 @@ class GitSubmoduleC:
8389
class GitSubmoduleUpdateOptionsC:
8490
fetch_opts: GitFetchOptionsC
8591

92+
class GitRemoteHeadC:
93+
local: int
94+
oid: GitOidC
95+
loid: GitOidC
96+
name: char_pointer
97+
symref_target: char_pointer
98+
8699
class UnsignedIntC:
87100
def __getitem__(self, item: Literal[0]) -> int: ...
88101

@@ -284,6 +297,12 @@ def new(a: Literal['git_signature *']) -> GitSignatureC: ...
284297
@overload
285298
def new(a: Literal['git_signature **']) -> _Pointer[GitSignatureC]: ...
286299
@overload
300+
def new(
301+
a: Literal['git_remote_head ***'],
302+
) -> _Pointer[_MultiPointer[GitRemoteHeadC]]: ...
303+
@overload
304+
def new(a: Literal['size_t *']) -> size_t: ...
305+
@overload
287306
def new(a: Literal['git_stash_save_options *']) -> GitStashSaveOptionsC: ...
288307
@overload
289308
def new(a: Literal['git_strarray *']) -> GitStrrayC: ...

pygit2/remotes.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525

2626
from __future__ import annotations
2727

28-
from typing import TYPE_CHECKING, Any
29-
30-
from . import utils
28+
from typing import TYPE_CHECKING, Any, TypedDict
3129

3230
# Import from pygit2
31+
from pygit2 import RemoteCallbacks
32+
33+
from . import utils
3334
from ._pygit2 import Oid
3435
from .callbacks import (
3536
git_fetch_options,
@@ -48,6 +49,14 @@
4849
from .repository import BaseRepository
4950

5051

52+
class LsRemotesDict(TypedDict):
53+
local: bool
54+
loid: None | Oid
55+
name: str | None
56+
symref_target: str | None
57+
oid: Oid
58+
59+
5160
class TransferProgress:
5261
"""Progress downloading and indexing data during a fetch."""
5362

@@ -180,7 +189,9 @@ def fetch(
180189

181190
return TransferProgress(C.git_remote_stats(self._remote))
182191

183-
def ls_remotes(self, callbacks=None, proxy=None):
192+
def ls_remotes(
193+
self, callbacks: RemoteCallbacks | None = None, proxy: str | None | bool = None
194+
) -> list[LsRemotesDict]:
184195
"""
185196
Return a list of dicts that maps to `git_remote_head` from a
186197
`ls_remotes` call.
@@ -209,13 +220,15 @@ def ls_remotes(self, callbacks=None, proxy=None):
209220
else:
210221
loid = None
211222

212-
remote = {
213-
'local': local,
214-
'loid': loid,
215-
'name': maybe_string(ref.name),
216-
'symref_target': maybe_string(ref.symref_target),
217-
'oid': Oid(raw=bytes(ffi.buffer(ref.oid.id)[:])),
218-
}
223+
remote = LsRemotesDict(
224+
{
225+
'local': local,
226+
'loid': loid,
227+
'name': maybe_string(ref.name),
228+
'symref_target': maybe_string(ref.symref_target),
229+
'oid': Oid(raw=bytes(ffi.buffer(ref.oid.id)[:])),
230+
}
231+
)
219232

220233
results.append(remote)
221234

@@ -256,7 +269,14 @@ def push_refspecs(self):
256269
check_error(err)
257270
return strarray_to_strings(specs)
258271

259-
def push(self, specs, callbacks=None, proxy=None, push_options=None, threads=1):
272+
def push(
273+
self,
274+
specs: list[str],
275+
callbacks: RemoteCallbacks | None = None,
276+
proxy: None | bool | str = None,
277+
push_options: None | list[str] = None,
278+
threads: int = 1,
279+
) -> None:
260280
"""
261281
Push the given refspec to the remote. Raises ``GitError`` on protocol
262282
error or unpack failure.
@@ -272,6 +292,8 @@ def push(self, specs, callbacks=None, proxy=None, push_options=None, threads=1):
272292
specs : [str]
273293
Push refspecs to use.
274294
295+
callbacks :
296+
275297
proxy : None or True or str
276298
Proxy configuration. Can be one of:
277299

test/test_remote.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,13 @@ def test_update_tips(emptyrepo: Repository) -> None:
282282
]
283283

284284
class MyCallbacks(pygit2.RemoteCallbacks):
285-
def __init__(self, tips):
285+
tips: list[tuple[str, pygit2.Oid, pygit2.Oid]]
286+
287+
def __init__(self, tips: list[tuple[str, pygit2.Oid, pygit2.Oid]]) -> None:
286288
self.tips = tips
287289
self.i = 0
288290

289-
def update_tips(self, name, old, new):
291+
def update_tips(self, name: str, old: pygit2.Oid, new: pygit2.Oid) -> None:
290292
assert self.tips[self.i] == (name, old, new)
291293
self.i += 1
292294

@@ -342,7 +344,7 @@ def clone(tmp_path: Path) -> Generator[Repository, None, None]:
342344

343345

344346
@pytest.fixture
345-
def remote(origin, clone):
347+
def remote(origin: Repository, clone: Repository) -> Generator[Remote, None, None]:
346348
yield clone.remotes.create('origin', origin.path)
347349

348350

0 commit comments

Comments
 (0)