Skip to content

more typing #1391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 31, 2025
5 changes: 5 additions & 0 deletions pygit2/_libgit2/ffi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ class GitRepositoryInitOptionsC:
class GitCloneOptionsC:
pass

class GitPackbuilderC:
pass

class GitProxyTC:
pass

Expand Down Expand Up @@ -275,6 +278,8 @@ def new(a: Literal['git_object *']) -> GitObjectC: ...
@overload
def new(a: Literal['git_object **']) -> _Pointer[GitObjectC]: ...
@overload
def new(a: Literal['git_packbuilder **']) -> _Pointer[GitPackbuilderC]: ...
@overload
def new(a: Literal['git_signature *']) -> GitSignatureC: ...
@overload
def new(a: Literal['git_signature **']) -> _Pointer[GitSignatureC]: ...
Expand Down
104 changes: 97 additions & 7 deletions pygit2/_pygit2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ from pathlib import Path
from queue import Queue
from threading import Event
from typing import (
Callable,
Generic,
Iterator,
Literal,
Expand Down Expand Up @@ -36,6 +37,7 @@ from .enums import (
BlobFilter,
BranchType,
CheckoutStrategy,
ConfigLevel,
DeltaStatus,
DescribeStrategy,
DiffFind,
Expand All @@ -57,6 +59,7 @@ from .enums import (
SortMode,
)
from .filter import Filter
from .packbuilder import PackBuilder
from .remotes import Remote
from .repository import BaseRepository
from .submodules import SubmoduleCollection
Expand Down Expand Up @@ -327,14 +330,20 @@ class _ObjectBase(Generic[T]):
tree: Tree
@overload
def peel(
self, target_type: 'Literal[GIT_OBJ_COMMIT] | Type[Commit]'
self, target_type: 'Literal[GIT_OBJ_COMMIT, ObjectType.COMMIT] | Type[Commit]'
) -> 'Commit': ...
@overload
def peel(self, target_type: 'Literal[GIT_OBJ_TREE] | Type[Tree]') -> 'Tree': ...
def peel(
self, target_type: 'Literal[GIT_OBJ_TREE, ObjectType.TREE] | Type[Tree]'
) -> 'Tree': ...
@overload
def peel(self, target_type: 'Literal[GIT_OBJ_TAG] | Type[Tag]') -> 'Tag': ...
def peel(
self, target_type: 'Literal[GIT_OBJ_TAG, ObjectType.TAG] | Type[Tag]'
) -> 'Tag': ...
@overload
def peel(self, target_type: 'Literal[GIT_OBJ_BLOB] | Type[Blob]') -> 'Blob': ...
def peel(
self, target_type: 'Literal[GIT_OBJ_BLOB, ObjectType.BLOB] | Type[Blob]'
) -> 'Blob': ...
@overload
def peel(self, target_type: 'None') -> 'Commit|Tree|Tag|Blob': ...
def read_raw(self) -> bytes: ...
Expand Down Expand Up @@ -553,6 +562,7 @@ class Note:
annotated_id: Oid
id: Oid
message: str
data: bytes
def remove(
self, author: Signature, committer: Signature, ref: str = 'refs/notes/commits'
) -> None: ...
Expand All @@ -561,9 +571,9 @@ class Odb:
backends: Iterator[OdbBackend]
def __init__(self, *args, **kwargs) -> None: ...
def add_backend(self, backend: OdbBackend, priority: int) -> None: ...
def add_disk_alternate(self, path: str) -> None: ...
def add_disk_alternate(self, path: str | Path) -> None: ...
def exists(self, oid: _OidArg) -> bool: ...
def read(self, oid: _OidArg) -> tuple[int, int, bytes]: ...
def read(self, oid: _OidArg) -> tuple[int, bytes]: ...
def write(self, type: int, data: bytes) -> Oid: ...
def __contains__(self, other: _OidArg) -> bool: ...
def __iter__(self) -> Iterator[Oid]: ... # Odb_as_iter
Expand Down Expand Up @@ -749,6 +759,7 @@ class Repository:
def _disown(self, *args, **kwargs) -> None: ...
@classmethod
def _from_c(cls, ptr: 'GitRepositoryC', owned: bool) -> 'Repository': ...
def __iter__(self) -> Iterator[Oid]: ...
def __getitem__(self, key: str | Oid) -> Object: ...
def add_worktree(self, name: str, path: str, ref: Reference = ...) -> Worktree: ...
def amend_commit(
Expand Down Expand Up @@ -877,6 +888,7 @@ class Repository:
def list_worktrees(self) -> list[str]: ...
def listall_branches(self, flag: BranchType = BranchType.LOCAL) -> list[str]: ...
def listall_mergeheads(self) -> list[Oid]: ...
def listall_references(self) -> list[str]: ...
def listall_stashes(self) -> list[Stash]: ...
def listall_submodules(self) -> list[str]: ...
def lookup_branch(
Expand Down Expand Up @@ -925,6 +937,12 @@ class Repository:
@property
def message(self) -> str: ...
def notes(self) -> Iterator[Note]: ...
def pack(
self,
path: str | Path | None = None,
pack_delegate: Callable[[PackBuilder], None] | None = None,
n_threads: int | None = None,
) -> bool: ...
def path_is_ignored(self, path: str) -> bool: ...
def raw_listall_branches(
self, flag: BranchType = BranchType.LOCAL
Expand Down Expand Up @@ -1070,7 +1088,79 @@ def discover_repository(
def hash(data: bytes) -> Oid: ...
def hashfile(path: str) -> Oid: ...
def init_file_backend(path: str, flags: int = 0) -> object: ...
def option(opt: Option, *args) -> None: ...
@overload
def option(
opt: Literal[
Option.GET_MWINDOW_FILE_LIMIT,
Option.GET_MWINDOW_MAPPED_LIMIT,
Option.GET_MWINDOW_SIZE,
],
) -> int: ...
@overload
def option(
opt: Literal[
Option.SET_MWINDOW_FILE_LIMIT,
Option.SET_MWINDOW_MAPPED_LIMIT,
Option.SET_MWINDOW_SIZE,
],
value: int,
) -> None: ...
@overload
def option(opt: Literal[Option.GET_SEARCH_PATH], level: ConfigLevel) -> str: ...
@overload
def option(
opt: Literal[Option.SET_SEARCH_PATH], level: ConfigLevel, value: str
) -> None: ...
@overload
def option(
opt: Literal[Option.SET_CACHE_OBJECT_LIMIT], object_type: ObjectType, limit: int
) -> None: ...
@overload
def option(opt: Literal[Option.SET_CACHE_MAX_SIZE], max_size: int) -> None: ...
@overload
def option(opt: Literal[Option.GET_CACHED_MEMORY]) -> tuple[int, int]: ...

# not implemented:
# Option.GET_TEMPLATE_PATH
# Option.SET_TEMPLATE_PATH

@overload
def option(
opt: Literal[Option.SET_SSL_CERT_LOCATIONS],
file: str | bytes | None,
dir: str | bytes | None,
) -> None: ...

# not implemented:
# Option.SET_USER_AGENT

@overload
def option(
opt: Literal[
Option.ENABLE_CACHING,
Option.ENABLE_STRICT_OBJECT_CREATION,
Option.ENABLE_STRICT_SYMBOLIC_REF_CREATION,
Option.ENABLE_OFS_DELTA,
Option.ENABLE_FSYNC_GITDIR,
Option.ENABLE_STRICT_HASH_VERIFICATION,
Option.ENABLE_UNSAVED_INDEX_SAFETY,
Option.DISABLE_PACK_KEEP_FILE_CHECKS,
Option.SET_OWNER_VALIDATION,
],
value: bool,
) -> None: ...
@overload
def option(opt: Literal[Option.GET_OWNER_VALIDATION]) -> int: ...

# not implemented:
# Option.SET_SSL_CIPHERS
# Option.GET_USER_AGENT
# Option.GET_WINDOWS_SHAREMODE
# Option.SET_WINDOWS_SHAREMODE
# Option.SET_ALLOCATOR
# Option.GET_PACK_MAX_OBJECTS
# Option.SET_PACK_MAX_OBJECTS

def reference_is_valid_name(refname: str) -> bool: ...
def tree_entry_cmp(a: Object, b: Object) -> int: ...
def _cache_enums() -> None: ...
Expand Down
29 changes: 17 additions & 12 deletions pygit2/packbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

from os import PathLike

# Import from pygit2
from pygit2 import Oid, Repository

from .errors import check_error
from .ffi import C, ffi
from .utils import to_bytes


class PackBuilder:
def __init__(self, repo):
def __init__(self, repo: Repository) -> None:
cpackbuilder = ffi.new('git_packbuilder **')
err = C.git_packbuilder_new(cpackbuilder, repo._repo)
check_error(err)
Expand All @@ -41,39 +44,41 @@ def __init__(self, repo):
self._cpackbuilder = cpackbuilder

@property
def _pointer(self):
def _pointer(self) -> bytes:
return bytes(ffi.buffer(self._packbuilder)[:])

def __del__(self):
def __del__(self) -> None:
C.git_packbuilder_free(self._packbuilder)

def __len__(self):
def __len__(self) -> int:
return C.git_packbuilder_object_count(self._packbuilder)

@staticmethod
def __convert_object_to_oid(oid):
def __convert_object_to_oid(oid: Oid) -> 'ffi.GitOidC':
git_oid = ffi.new('git_oid *')
ffi.buffer(git_oid)[:] = oid.raw[:]
return git_oid

def add(self, oid):
def add(self, oid: Oid) -> None:
git_oid = self.__convert_object_to_oid(oid)
err = C.git_packbuilder_insert(self._packbuilder, git_oid, ffi.NULL)
check_error(err)

def add_recur(self, oid):
def add_recur(self, oid: Oid) -> None:
git_oid = self.__convert_object_to_oid(oid)
err = C.git_packbuilder_insert_recur(self._packbuilder, git_oid, ffi.NULL)
check_error(err)

def set_threads(self, n_threads):
def set_threads(self, n_threads: int) -> int:
return C.git_packbuilder_set_threads(self._packbuilder, n_threads)

def write(self, path=None):
path = ffi.NULL if path is None else to_bytes(path)
err = C.git_packbuilder_write(self._packbuilder, path, 0, ffi.NULL, ffi.NULL)
def write(self, path: str | bytes | PathLike[str] | None = None) -> None:
path_bytes = ffi.NULL if path is None else to_bytes(path)
err = C.git_packbuilder_write(
self._packbuilder, path_bytes, 0, ffi.NULL, ffi.NULL
)
check_error(err)

@property
def written_objects_count(self):
def written_objects_count(self) -> int:
return C.git_packbuilder_written(self._packbuilder)
Loading
Loading