Skip to content

Commit 71e28b8

Browse files
committed
add types to git.db and git.exc
1 parent a094ac1 commit 71e28b8

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

Diff for: git/db.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
)
77
from gitdb.db import GitDB # @UnusedImport
88
from gitdb.db import LooseObjectDB
9+
from gitdb.exc import BadObject
910

10-
from .exc import (
11-
GitCommandError,
12-
BadObject
13-
)
11+
from .exc import GitCommandError
12+
13+
# typing-------------------------------------------------
14+
15+
from .cmd import Git
16+
from .types import PathLike, TBD
1417

18+
# --------------------------------------------------------
1519

1620
__all__ = ('GitCmdObjectDB', 'GitDB')
1721

@@ -28,23 +32,23 @@ class GitCmdObjectDB(LooseObjectDB):
2832
have packs and the other implementations
2933
"""
3034

31-
def __init__(self, root_path, git):
35+
def __init__(self, root_path: PathLike, git: Git) -> None:
3236
"""Initialize this instance with the root and a git command"""
3337
super(GitCmdObjectDB, self).__init__(root_path)
3438
self._git = git
3539

36-
def info(self, sha):
40+
def info(self, sha: bytes) -> OInfo:
3741
hexsha, typename, size = self._git.get_object_header(bin_to_hex(sha))
3842
return OInfo(hex_to_bin(hexsha), typename, size)
3943

40-
def stream(self, sha):
44+
def stream(self, sha: bytes) -> OStream:
4145
"""For now, all lookup is done by git itself"""
4246
hexsha, typename, size, stream = self._git.stream_object_data(bin_to_hex(sha))
4347
return OStream(hex_to_bin(hexsha), typename, size, stream)
4448

4549
# { Interface
4650

47-
def partial_to_complete_sha_hex(self, partial_hexsha):
51+
def partial_to_complete_sha_hex(self, partial_hexsha: str) -> bytes:
4852
""":return: Full binary 20 byte sha from the given partial hexsha
4953
:raise AmbiguousObjectName:
5054
:raise BadObject:

Diff for: git/exc.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
from gitdb.exc import * # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614
99
from git.compat import safe_decode
1010

11+
# typing ----------------------------------------------------
12+
13+
from git.repo.base import Repo
14+
from git.types import PathLike
15+
from typing import IO, List, Optional, Sequence, Tuple, Union
16+
17+
# ------------------------------------------------------------------
1118

1219
class GitError(Exception):
1320
""" Base class for all package exceptions """
@@ -37,7 +44,9 @@ class CommandError(GitError):
3744
#: "'%s' failed%s"
3845
_msg = "Cmd('%s') failed%s"
3946

40-
def __init__(self, command, status=None, stderr=None, stdout=None):
47+
def __init__(self, command: Union[List[str], Tuple[str, ...], str],
48+
status: Union[str, None, Exception] = None,
49+
stderr: Optional[IO[str]] = None, stdout: Optional[IO[str]] = None) -> None:
4150
if not isinstance(command, (tuple, list)):
4251
command = command.split()
4352
self.command = command
@@ -53,28 +62,33 @@ def __init__(self, command, status=None, stderr=None, stdout=None):
5362
status = "'%s'" % s if isinstance(status, str) else s
5463

5564
self._cmd = safe_decode(command[0])
56-
self._cmdline = ' '.join(safe_decode(i) for i in command)
65+
self._cmdline = ' '.join(str(safe_decode(i)) for i in command)
5766
self._cause = status and " due to: %s" % status or "!"
58-
self.stdout = stdout and "\n stdout: '%s'" % safe_decode(stdout) or ''
59-
self.stderr = stderr and "\n stderr: '%s'" % safe_decode(stderr) or ''
67+
self.stdout = stdout and "\n stdout: '%s'" % safe_decode(str(stdout)) or ''
68+
self.stderr = stderr and "\n stderr: '%s'" % safe_decode(str(stderr)) or ''
6069

61-
def __str__(self):
70+
def __str__(self) -> str:
6271
return (self._msg + "\n cmdline: %s%s%s") % (
6372
self._cmd, self._cause, self._cmdline, self.stdout, self.stderr)
6473

6574

6675
class GitCommandNotFound(CommandError):
6776
"""Thrown if we cannot find the `git` executable in the PATH or at the path given by
6877
the GIT_PYTHON_GIT_EXECUTABLE environment variable"""
69-
def __init__(self, command, cause):
78+
79+
def __init__(self, command: Union[List[str], Tuple[str], str], cause: Union[str, Exception]) -> None:
7080
super(GitCommandNotFound, self).__init__(command, cause)
7181
self._msg = "Cmd('%s') not found%s"
7282

7383

7484
class GitCommandError(CommandError):
7585
""" Thrown if execution of the git command fails with non-zero status code. """
7686

77-
def __init__(self, command, status, stderr=None, stdout=None):
87+
def __init__(self, command: Union[List[str], Tuple[str, ...], str],
88+
status: Union[str, None, Exception] = None,
89+
stderr: Optional[IO[str]] = None,
90+
stdout: Optional[IO[str]] = None,
91+
) -> None:
7892
super(GitCommandError, self).__init__(command, status, stderr, stdout)
7993

8094

@@ -92,13 +106,13 @@ class CheckoutError(GitError):
92106
were checked out successfully and hence match the version stored in the
93107
index"""
94108

95-
def __init__(self, message, failed_files, valid_files, failed_reasons):
109+
def __init__(self, message: str, failed_files: List[PathLike], valid_files: List[PathLike], failed_reasons: List[str]) -> None:
96110
Exception.__init__(self, message)
97111
self.failed_files = failed_files
98112
self.failed_reasons = failed_reasons
99113
self.valid_files = valid_files
100114

101-
def __str__(self):
115+
def __str__(self) -> str:
102116
return Exception.__str__(self) + ":%s" % self.failed_files
103117

104118

@@ -116,17 +130,18 @@ class HookExecutionError(CommandError):
116130
"""Thrown if a hook exits with a non-zero exit code. It provides access to the exit code and the string returned
117131
via standard output"""
118132

119-
def __init__(self, command, status, stderr=None, stdout=None):
133+
def __init__(self, command: Union[List[str], Tuple[str, ...], str], status: Optional[str],
134+
stderr: Optional[IO[str]] = None, stdout: Optional[IO[str]] = None) -> None:
120135
super(HookExecutionError, self).__init__(command, status, stderr, stdout)
121136
self._msg = "Hook('%s') failed%s"
122137

123138

124139
class RepositoryDirtyError(GitError):
125140
"""Thrown whenever an operation on a repository fails as it has uncommitted changes that would be overwritten"""
126141

127-
def __init__(self, repo, message):
142+
def __init__(self, repo: Repo, message: str) -> None:
128143
self.repo = repo
129144
self.message = message
130145

131-
def __str__(self):
146+
def __str__(self) -> str:
132147
return "Operation cannot be performed on %r: %s" % (self.repo, self.message)

Diff for: mypy.ini

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
[mypy]
33

44
disallow_untyped_defs = True
5+
6+
mypy_path = 'git'

0 commit comments

Comments
 (0)