Skip to content

Commit

Permalink
More typing
Browse files Browse the repository at this point in the history
  • Loading branch information
sathieu committed Feb 16, 2025
1 parent eba0249 commit 6a4192a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
8 changes: 4 additions & 4 deletions pygit2/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@


class Payload:
def __init__(self, **kw):
def __init__(self, **kw: object):
for key, value in kw.items():
setattr(self, key, value)
self._stored_exception = None

def check_error(self, error_code):
def check_error(self, error_code: int) -> None:
if error_code == C.GIT_EUSER:
assert self._stored_exception is not None
raise self._stored_exception
Expand Down Expand Up @@ -120,7 +120,7 @@ def __init__(self, credentials=None, certificate_check=None):
if certificate_check is not None:
self.certificate_check = certificate_check

def sideband_progress(self, string):
def sideband_progress(self, string: str) -> None:
"""
Progress output callback. Override this function with your own
progress reporting function
Expand Down Expand Up @@ -159,7 +159,7 @@ def credentials(
"""
raise Passthrough

def certificate_check(self, certificate, valid, host):
def certificate_check(self, certificate: None, valid: bool, host: str) -> bool:
"""
Certificate callback. Override with your own function to determine
whether to accept the server's certificate.
Expand Down
30 changes: 23 additions & 7 deletions pygit2/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,25 @@
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

from __future__ import annotations

from typing import TYPE_CHECKING

from .enums import CredentialType


if TYPE_CHECKING:
from pathlib import Path


class Username:
"""Username credentials
This is an object suitable for passing to a remote's credentials
callback and for returning from said callback.
"""

def __init__(self, username):
def __init__(self, username: str):
self._username = username

@property
Expand All @@ -44,7 +52,9 @@ def credential_type(self) -> CredentialType:
def credential_tuple(self):
return (self._username,)

def __call__(self, _url, _username, _allowed):
def __call__(
self, _url: str, _username: str | None, _allowed: CredentialType
) -> Username:
return self


Expand All @@ -55,7 +65,7 @@ class UserPass:
callback and for returning from said callback.
"""

def __init__(self, username, password):
def __init__(self, username: str, password: str):
self._username = username
self._password = password

Expand All @@ -67,7 +77,9 @@ def credential_type(self) -> CredentialType:
def credential_tuple(self):
return (self._username, self._password)

def __call__(self, _url, _username, _allowed):
def __call__(
self, _url: str, _username: str | None, _allowed: CredentialType
) -> UserPass:
return self


Expand All @@ -94,7 +106,9 @@ class Keypair:
no passphrase is required.
"""

def __init__(self, username, pubkey, privkey, passphrase):
def __init__(
self, username: str, pubkey: str | Path, privkey: str | Path, passphrase: str
):
self._username = username
self._pubkey = pubkey
self._privkey = privkey
Expand All @@ -108,12 +122,14 @@ def credential_type(self) -> CredentialType:
def credential_tuple(self):
return (self._username, self._pubkey, self._privkey, self._passphrase)

def __call__(self, _url, _username, _allowed):
def __call__(
self, _url: str, _username: str | None, _allowed: CredentialType
) -> Keypair:
return self


class KeypairFromAgent(Keypair):
def __init__(self, username):
def __init__(self, username: str):
super().__init__(username, None, None, None)


Expand Down
2 changes: 1 addition & 1 deletion pygit2/remotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def names(self):
for name in self._ffi_names():
yield maybe_string(name)

def create(self, name, url, fetch=None):
def create(self, name, url, fetch=None) -> Remote:
"""Create a new remote with the given name and url. Returns a <Remote>
object.
Expand Down

0 comments on commit 6a4192a

Please sign in to comment.