diff options
author | yobmod <yobmod@gmail.com> | 2021-03-01 20:55:08 +0000 |
---|---|---|
committer | yobmod <yobmod@gmail.com> | 2021-03-01 20:55:08 +0000 |
commit | 71e28b8e2ac1b8bc8990454721740b2073829110 (patch) | |
tree | 1e15da7683582b45d69caa59be9e6ba3caf81bed | |
parent | a094ac1808f7c5fa0653ac075074bb2232223ac1 (diff) | |
download | gitpython-71e28b8e2ac1b8bc8990454721740b2073829110.tar.gz |
add types to git.db and git.exc
-rw-r--r-- | git/db.py | 20 | ||||
-rw-r--r-- | git/exc.py | 39 | ||||
-rw-r--r-- | mypy.ini | 2 |
3 files changed, 41 insertions, 20 deletions
@@ -6,12 +6,16 @@ from gitdb.base import ( ) from gitdb.db import GitDB # @UnusedImport from gitdb.db import LooseObjectDB +from gitdb.exc import BadObject -from .exc import ( - GitCommandError, - BadObject -) +from .exc import GitCommandError + +# typing------------------------------------------------- + +from .cmd import Git +from .types import PathLike, TBD +# -------------------------------------------------------- __all__ = ('GitCmdObjectDB', 'GitDB') @@ -28,23 +32,23 @@ class GitCmdObjectDB(LooseObjectDB): have packs and the other implementations """ - def __init__(self, root_path, git): + def __init__(self, root_path: PathLike, git: Git) -> None: """Initialize this instance with the root and a git command""" super(GitCmdObjectDB, self).__init__(root_path) self._git = git - def info(self, sha): + def info(self, sha: bytes) -> OInfo: hexsha, typename, size = self._git.get_object_header(bin_to_hex(sha)) return OInfo(hex_to_bin(hexsha), typename, size) - def stream(self, sha): + def stream(self, sha: bytes) -> OStream: """For now, all lookup is done by git itself""" hexsha, typename, size, stream = self._git.stream_object_data(bin_to_hex(sha)) return OStream(hex_to_bin(hexsha), typename, size, stream) # { Interface - def partial_to_complete_sha_hex(self, partial_hexsha): + def partial_to_complete_sha_hex(self, partial_hexsha: str) -> bytes: """:return: Full binary 20 byte sha from the given partial hexsha :raise AmbiguousObjectName: :raise BadObject: @@ -8,6 +8,13 @@ from gitdb.exc import * # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614 from git.compat import safe_decode +# typing ---------------------------------------------------- + +from git.repo.base import Repo +from git.types import PathLike +from typing import IO, List, Optional, Sequence, Tuple, Union + +# ------------------------------------------------------------------ class GitError(Exception): """ Base class for all package exceptions """ @@ -37,7 +44,9 @@ class CommandError(GitError): #: "'%s' failed%s" _msg = "Cmd('%s') failed%s" - def __init__(self, command, status=None, stderr=None, stdout=None): + def __init__(self, command: Union[List[str], Tuple[str, ...], str], + status: Union[str, None, Exception] = None, + stderr: Optional[IO[str]] = None, stdout: Optional[IO[str]] = None) -> None: if not isinstance(command, (tuple, list)): command = command.split() self.command = command @@ -53,12 +62,12 @@ class CommandError(GitError): status = "'%s'" % s if isinstance(status, str) else s self._cmd = safe_decode(command[0]) - self._cmdline = ' '.join(safe_decode(i) for i in command) + self._cmdline = ' '.join(str(safe_decode(i)) for i in command) self._cause = status and " due to: %s" % status or "!" - self.stdout = stdout and "\n stdout: '%s'" % safe_decode(stdout) or '' - self.stderr = stderr and "\n stderr: '%s'" % safe_decode(stderr) or '' + self.stdout = stdout and "\n stdout: '%s'" % safe_decode(str(stdout)) or '' + self.stderr = stderr and "\n stderr: '%s'" % safe_decode(str(stderr)) or '' - def __str__(self): + def __str__(self) -> str: return (self._msg + "\n cmdline: %s%s%s") % ( self._cmd, self._cause, self._cmdline, self.stdout, self.stderr) @@ -66,7 +75,8 @@ class CommandError(GitError): class GitCommandNotFound(CommandError): """Thrown if we cannot find the `git` executable in the PATH or at the path given by the GIT_PYTHON_GIT_EXECUTABLE environment variable""" - def __init__(self, command, cause): + + def __init__(self, command: Union[List[str], Tuple[str], str], cause: Union[str, Exception]) -> None: super(GitCommandNotFound, self).__init__(command, cause) self._msg = "Cmd('%s') not found%s" @@ -74,7 +84,11 @@ class GitCommandNotFound(CommandError): class GitCommandError(CommandError): """ Thrown if execution of the git command fails with non-zero status code. """ - def __init__(self, command, status, stderr=None, stdout=None): + def __init__(self, command: Union[List[str], Tuple[str, ...], str], + status: Union[str, None, Exception] = None, + stderr: Optional[IO[str]] = None, + stdout: Optional[IO[str]] = None, + ) -> None: super(GitCommandError, self).__init__(command, status, stderr, stdout) @@ -92,13 +106,13 @@ class CheckoutError(GitError): were checked out successfully and hence match the version stored in the index""" - def __init__(self, message, failed_files, valid_files, failed_reasons): + def __init__(self, message: str, failed_files: List[PathLike], valid_files: List[PathLike], failed_reasons: List[str]) -> None: Exception.__init__(self, message) self.failed_files = failed_files self.failed_reasons = failed_reasons self.valid_files = valid_files - def __str__(self): + def __str__(self) -> str: return Exception.__str__(self) + ":%s" % self.failed_files @@ -116,7 +130,8 @@ class HookExecutionError(CommandError): """Thrown if a hook exits with a non-zero exit code. It provides access to the exit code and the string returned via standard output""" - def __init__(self, command, status, stderr=None, stdout=None): + def __init__(self, command: Union[List[str], Tuple[str, ...], str], status: Optional[str], + stderr: Optional[IO[str]] = None, stdout: Optional[IO[str]] = None) -> None: super(HookExecutionError, self).__init__(command, status, stderr, stdout) self._msg = "Hook('%s') failed%s" @@ -124,9 +139,9 @@ class HookExecutionError(CommandError): class RepositoryDirtyError(GitError): """Thrown whenever an operation on a repository fails as it has uncommitted changes that would be overwritten""" - def __init__(self, repo, message): + def __init__(self, repo: Repo, message: str) -> None: self.repo = repo self.message = message - def __str__(self): + def __str__(self) -> str: return "Operation cannot be performed on %r: %s" % (self.repo, self.message) @@ -2,3 +2,5 @@ [mypy] disallow_untyped_defs = True + +mypy_path = 'git' |