Skip to content
Open
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
55bc286
add some rules to ruff
DinhHuy2010 Dec 21, 2024
2c06530
ignore unused imports
DinhHuy2010 Dec 21, 2024
484886a
Initial formatting and liniting fix of all files
DinhHuy2010 Dec 21, 2024
6330d4b
add type checking configuration (use pyright)
DinhHuy2010 Dec 21, 2024
325891b
add _libgit2.pyi
DinhHuy2010 Dec 21, 2024
aa37e2d
mega fix _pygit2.pyi
DinhHuy2010 Dec 21, 2024
b53b9a7
fix stubs
DinhHuy2010 Dec 21, 2024
c70241d
change lib to Any
DinhHuy2010 Dec 21, 2024
42b5129
fix pyright config
DinhHuy2010 Dec 21, 2024
5f6657e
fix _pygit2.pyi again
DinhHuy2010 Dec 21, 2024
4b3878b
fix enums.py
DinhHuy2010 Dec 21, 2024
49bc71b
add FilterSource
DinhHuy2010 Dec 21, 2024
7433683
yet another fix the _pygit2.pyi
DinhHuy2010 Dec 21, 2024
3aabbc7
add typing to credentials.py
DinhHuy2010 Dec 21, 2024
9a1affc
add typing to errors.py
DinhHuy2010 Dec 21, 2024
735ba5a
fix pyproject again
DinhHuy2010 Dec 21, 2024
5fc3b70
add filter_* functions to _pygit2.pyi
DinhHuy2010 Dec 21, 2024
95b003f
add typing to utils.py
DinhHuy2010 Dec 21, 2024
e71d03b
fix ffi.py
DinhHuy2010 Dec 21, 2024
9b68da8
initial commit of pygit2 __init__
DinhHuy2010 Dec 21, 2024
7afd694
remove pygit2 star imports
DinhHuy2010 Dec 22, 2024
b2680c3
fix pyproject again
DinhHuy2010 Dec 22, 2024
4970a94
fix pyproject
DinhHuy2010 Dec 22, 2024
40f44c1
fix pyproject again
DinhHuy2010 Dec 22, 2024
e3ed43f
initial commit of pygit2 callbacks.py
DinhHuy2010 Dec 22, 2024
35fb5e1
typing fix for __init__.py
DinhHuy2010 Dec 22, 2024
c3b01f8
fix typing for _build.py and _run.py
DinhHuy2010 Dec 24, 2024
0fe1aac
fix pyproject.toml
DinhHuy2010 Dec 25, 2024
58b88c2
add typing for packbuilder.py
DinhHuy2010 Dec 25, 2024
d8755fb
fix typing for utils
DinhHuy2010 Dec 25, 2024
8114f2b
hotfix for packbuilder.py
DinhHuy2010 Dec 25, 2024
071df9d
fix _pygit2.pyi
DinhHuy2010 Dec 25, 2024
9a10a41
initial stub for repository.py
DinhHuy2010 Dec 25, 2024
5f2d9ae
add typing for branches.py
DinhHuy2010 Dec 29, 2024
720881e
add typing to blob.py and fix _pygit2.pyi
DinhHuy2010 Dec 30, 2024
0e55fb3
add typing for blame.py and add _ctyping
DinhHuy2010 Dec 30, 2024
6ab13f7
Resolve confilcts from 'upstream/master'
DinhHuy2010 Dec 30, 2024
5dca60a
change pathtype name to StrOrBytesPath
DinhHuy2010 Dec 30, 2024
599f2b7
fix _ctyping
DinhHuy2010 Dec 30, 2024
d3be17a
fix _ctyping again
DinhHuy2010 Dec 30, 2024
dddd720
add maybe_bytes
DinhHuy2010 Dec 30, 2024
f019420
add typing to config.py
DinhHuy2010 Jan 13, 2025
86c62cd
Merge branch 'master' of https://github.com/libgit2/pygit2 into typec…
DinhHuy2010 Jan 13, 2025
b0da06b
fix typing
DinhHuy2010 Jan 13, 2025
0fab374
Solve confilcts
DinhHuy2010 Feb 17, 2025
8b46dec
Merge branch 'master' into typecheck
DinhHuy2010 Feb 17, 2025
48e0dce
fix config
DinhHuy2010 Feb 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix typing for utils
  • Loading branch information
DinhHuy2010 committed Dec 25, 2024
commit d8755fb005d1b95e22ebdd593a1daeb2e4ddb8df
35 changes: 27 additions & 8 deletions pygit2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@
import contextlib
import os
from types import TracebackType
from typing import TYPE_CHECKING, Any, Generic, TypeVar, cast
from typing import TYPE_CHECKING, Any, Generic, TypeAlias, TypeVar, cast, overload

# Import from pygit2
from .ffi import C, ffi

if TYPE_CHECKING:
from _cffi_backend import _CDataBase as CData
from _typeshed import SupportsLenAndGetItem

_T = TypeVar('_T')
StrOrBytesOrPathLike: TypeAlias = str | bytes | os.PathLike[str] | os.PathLike[bytes]


def maybe_string(ptr: Any) -> str | None:
def maybe_string(ptr: CData | Any) -> str | None:
if not ptr:
return None

Expand All @@ -49,20 +51,33 @@ def maybe_string(ptr: Any) -> str | None:
return out


def to_bytes(s: Any, encoding: str = 'utf-8', errors: str = 'strict') -> bytes | Any:
# Cannot describe ffi.NULL, so using CData


@overload
def to_bytes(s: CData | None) -> CData: ...
@overload
def to_bytes(s: StrOrBytesOrPathLike) -> bytes: ...


def to_bytes(
s: StrOrBytesOrPathLike | CData | None,
encoding: str = 'utf-8',
errors: str = 'strict',
) -> bytes | CData:
if s == ffi.NULL or s is None:
return ffi.NULL

if hasattr(s, '__fspath__'):
if isinstance(s, os.PathLike):
s = os.fspath(s)

if isinstance(s, bytes):
return s

return s.encode(encoding, errors)
return cast(str, s).encode(encoding, errors)


def to_str(s: Any) -> str:
def to_str(s: StrOrBytesOrPathLike) -> str:
if hasattr(s, '__fspath__'):
s = os.fspath(s)

Expand All @@ -75,14 +90,18 @@ def to_str(s: Any) -> str:
raise TypeError(f'unexpected type "{repr(s)}"')


def buffer_to_bytes(cdata: CData) -> bytes:
buf = ffi.buffer(cdata)
return cast(bytes, buf[:])


def ptr_to_bytes(ptr_cdata: Any) -> bytes:
"""
Convert a pointer coming from C code (<cdata 'some_type *'>)
to a byte buffer containing the address that the pointer refers to.
"""

pp = ffi.new('void **', ptr_cdata)
return bytes(ffi.buffer(pp)[:]) # type: ignore
return buffer_to_bytes(ffi.new('void **', ptr_cdata))


@contextlib.contextmanager
Expand Down