Skip to content

Commit 6a4e860

Browse files
committed
improve types according to test/test_packbuilder.py
1 parent 551af8d commit 6a4e860

File tree

5 files changed

+47
-22
lines changed

5 files changed

+47
-22
lines changed

pygit2/_libgit2/ffi.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ class GitRepositoryInitOptionsC:
192192
class GitCloneOptionsC:
193193
pass
194194

195+
class GitPackbuilderC:
196+
pass
197+
195198
class GitProxyTC:
196199
pass
197200

@@ -275,6 +278,8 @@ def new(a: Literal['git_object *']) -> GitObjectC: ...
275278
@overload
276279
def new(a: Literal['git_object **']) -> _Pointer[GitObjectC]: ...
277280
@overload
281+
def new(a: Literal['git_packbuilder **']) -> _Pointer[GitPackbuilderC]: ...
282+
@overload
278283
def new(a: Literal['git_signature *']) -> GitSignatureC: ...
279284
@overload
280285
def new(a: Literal['git_signature **']) -> _Pointer[GitSignatureC]: ...

pygit2/_pygit2.pyi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from pathlib import Path
55
from queue import Queue
66
from threading import Event
77
from typing import (
8+
Callable,
89
Generic,
910
Iterator,
1011
Literal,
@@ -58,6 +59,7 @@ from .enums import (
5859
SortMode,
5960
)
6061
from .filter import Filter
62+
from .packbuilder import PackBuilder
6163
from .remotes import Remote
6264
from .repository import BaseRepository
6365
from .submodules import SubmoduleCollection
@@ -757,6 +759,7 @@ class Repository:
757759
def _disown(self, *args, **kwargs) -> None: ...
758760
@classmethod
759761
def _from_c(cls, ptr: 'GitRepositoryC', owned: bool) -> 'Repository': ...
762+
def __iter__(self) -> Iterator[Oid]: ...
760763
def __getitem__(self, key: str | Oid) -> Object: ...
761764
def add_worktree(self, name: str, path: str, ref: Reference = ...) -> Worktree: ...
762765
def amend_commit(
@@ -934,6 +937,12 @@ class Repository:
934937
@property
935938
def message(self) -> str: ...
936939
def notes(self) -> Iterator[Note]: ...
940+
def pack(
941+
self,
942+
path: str | Path | None = None,
943+
pack_delegate: Callable[[PackBuilder], None] | None = None,
944+
n_threads: int | None = None,
945+
) -> bool: ...
937946
def path_is_ignored(self, path: str) -> bool: ...
938947
def raw_listall_branches(
939948
self, flag: BranchType = BranchType.LOCAL

pygit2/packbuilder.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@
2323
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
2424
# Boston, MA 02110-1301, USA.
2525

26+
from os import PathLike
2627

2728
# Import from pygit2
29+
from pygit2 import Oid, Repository
30+
2831
from .errors import check_error
2932
from .ffi import C, ffi
3033
from .utils import to_bytes
3134

3235

3336
class PackBuilder:
34-
def __init__(self, repo):
37+
def __init__(self, repo: Repository) -> None:
3538
cpackbuilder = ffi.new('git_packbuilder **')
3639
err = C.git_packbuilder_new(cpackbuilder, repo._repo)
3740
check_error(err)
@@ -41,39 +44,41 @@ def __init__(self, repo):
4144
self._cpackbuilder = cpackbuilder
4245

4346
@property
44-
def _pointer(self):
47+
def _pointer(self) -> bytes:
4548
return bytes(ffi.buffer(self._packbuilder)[:])
4649

47-
def __del__(self):
50+
def __del__(self) -> None:
4851
C.git_packbuilder_free(self._packbuilder)
4952

50-
def __len__(self):
53+
def __len__(self) -> int:
5154
return C.git_packbuilder_object_count(self._packbuilder)
5255

5356
@staticmethod
54-
def __convert_object_to_oid(oid):
57+
def __convert_object_to_oid(oid: Oid) -> 'ffi.GitOidC':
5558
git_oid = ffi.new('git_oid *')
5659
ffi.buffer(git_oid)[:] = oid.raw[:]
5760
return git_oid
5861

59-
def add(self, oid):
62+
def add(self, oid: Oid) -> None:
6063
git_oid = self.__convert_object_to_oid(oid)
6164
err = C.git_packbuilder_insert(self._packbuilder, git_oid, ffi.NULL)
6265
check_error(err)
6366

64-
def add_recur(self, oid):
67+
def add_recur(self, oid: Oid) -> None:
6568
git_oid = self.__convert_object_to_oid(oid)
6669
err = C.git_packbuilder_insert_recur(self._packbuilder, git_oid, ffi.NULL)
6770
check_error(err)
6871

69-
def set_threads(self, n_threads):
72+
def set_threads(self, n_threads: int) -> int:
7073
return C.git_packbuilder_set_threads(self._packbuilder, n_threads)
7174

72-
def write(self, path=None):
73-
path = ffi.NULL if path is None else to_bytes(path)
74-
err = C.git_packbuilder_write(self._packbuilder, path, 0, ffi.NULL, ffi.NULL)
75+
def write(self, path: str | bytes | PathLike[str] | None = None) -> None:
76+
path_bytes = ffi.NULL if path is None else to_bytes(path)
77+
err = C.git_packbuilder_write(
78+
self._packbuilder, path_bytes, 0, ffi.NULL, ffi.NULL
79+
)
7580
check_error(err)
7681

7782
@property
78-
def written_objects_count(self):
83+
def written_objects_count(self) -> int:
7984
return C.git_packbuilder_written(self._packbuilder)

test/test_packbuilder.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,21 @@
2626
"""Tests for Index files."""
2727

2828
from pathlib import Path
29+
from typing import Callable
2930

3031
import pygit2
31-
from pygit2 import PackBuilder
32+
from pygit2 import Oid, PackBuilder, Repository
3233

3334
from . import utils
3435

3536

36-
def test_create_packbuilder(testrepo):
37+
def test_create_packbuilder(testrepo: Repository) -> None:
3738
# simple test of PackBuilder creation
3839
packbuilder = PackBuilder(testrepo)
3940
assert len(packbuilder) == 0
4041

4142

42-
def test_add(testrepo):
43+
def test_add(testrepo: Repository) -> None:
4344
# Add a few objects and confirm that the count is correct
4445
packbuilder = PackBuilder(testrepo)
4546
objects_to_add = [obj for obj in testrepo]
@@ -49,9 +50,10 @@ def test_add(testrepo):
4950
assert len(packbuilder) == 2
5051

5152

52-
def test_add_recursively(testrepo):
53+
def test_add_recursively(testrepo: Repository) -> None:
5354
# Add the head object and referenced objects recursively and confirm that the count is correct
5455
packbuilder = PackBuilder(testrepo)
56+
assert isinstance(testrepo.head.target, Oid)
5557
packbuilder.add_recur(testrepo.head.target)
5658

5759
# expect a count of 4 made up of the following referenced objects:
@@ -63,14 +65,14 @@ def test_add_recursively(testrepo):
6365
assert len(packbuilder) == 4
6466

6567

66-
def test_repo_pack(testrepo, tmp_path):
68+
def test_repo_pack(testrepo: Repository, tmp_path: Path) -> None:
6769
# pack the repo with the default strategy
6870
confirm_same_repo_after_packing(testrepo, tmp_path, None)
6971

7072

71-
def test_pack_with_delegate(testrepo, tmp_path):
73+
def test_pack_with_delegate(testrepo: Repository, tmp_path: Path) -> None:
7274
# loop through all branches and add each commit to the packbuilder
73-
def pack_delegate(pb):
75+
def pack_delegate(pb: PackBuilder) -> None:
7476
for branch in pb._repo.branches:
7577
br = pb._repo.branches.get(branch)
7678
for commit in br.log():
@@ -79,15 +81,19 @@ def pack_delegate(pb):
7981
confirm_same_repo_after_packing(testrepo, tmp_path, pack_delegate)
8082

8183

82-
def setup_second_repo(tmp_path):
84+
def setup_second_repo(tmp_path: Path) -> Repository:
8385
# helper method to set up a second repo for comparison
8486
tmp_path_2 = tmp_path / 'test_repo2'
8587
with utils.TemporaryRepository('testrepo.zip', tmp_path_2) as path:
8688
testrepo = pygit2.Repository(path)
8789
return testrepo
8890

8991

90-
def confirm_same_repo_after_packing(testrepo, tmp_path, pack_delegate):
92+
def confirm_same_repo_after_packing(
93+
testrepo: Repository,
94+
tmp_path: Path,
95+
pack_delegate: Callable[[PackBuilder], None] | None,
96+
) -> None:
9197
# Helper method to confirm the contents of two repos before and after packing
9298
pack_repo = setup_second_repo(tmp_path)
9399
pack_repo_path = Path(pack_repo.path)

test/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def force_rm_handle(remove_path, path, excinfo):
8989
remove_path(path)
9090

9191

92-
def rmtree(path):
92+
def rmtree(path: str | Path) -> None:
9393
"""In Windows a read-only file cannot be removed, and shutil.rmtree fails.
9494
So we implement our own version of rmtree to address this issue.
9595
"""

0 commit comments

Comments
 (0)