Skip to content

Commit 3435632

Browse files
committedMay 13, 2021
Merge branch 'addtypes'
·
3.1.443.1.16
2 parents 2448ac4 + fed0cad commit 3435632

File tree

8 files changed

+390
-207
lines changed

8 files changed

+390
-207
lines changed
 

‎git/cmd.py

Lines changed: 212 additions & 96 deletions
Large diffs are not rendered by default.

‎git/compat.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@
4343
@overload
4444
def safe_decode(s: None) -> None: ...
4545

46+
4647
@overload
47-
def safe_decode(s: Union[IO[str], AnyStr]) -> str: ...
48+
def safe_decode(s: AnyStr) -> str: ...
49+
4850

49-
def safe_decode(s: Union[IO[str], AnyStr, None]) -> Optional[str]:
51+
def safe_decode(s: Union[AnyStr, None]) -> Optional[str]:
5052
"""Safely decodes a binary string to unicode"""
5153
if isinstance(s, str):
5254
return s
@@ -61,9 +63,11 @@ def safe_decode(s: Union[IO[str], AnyStr, None]) -> Optional[str]:
6163
@overload
6264
def safe_encode(s: None) -> None: ...
6365

66+
6467
@overload
6568
def safe_encode(s: AnyStr) -> bytes: ...
6669

70+
6771
def safe_encode(s: Optional[AnyStr]) -> Optional[bytes]:
6872
"""Safely encodes a binary string to unicode"""
6973
if isinstance(s, str):
@@ -79,9 +83,11 @@ def safe_encode(s: Optional[AnyStr]) -> Optional[bytes]:
7983
@overload
8084
def win_encode(s: None) -> None: ...
8185

86+
8287
@overload
8388
def win_encode(s: AnyStr) -> bytes: ...
8489

90+
8591
def win_encode(s: Optional[AnyStr]) -> Optional[bytes]:
8692
"""Encode unicodes for process arguments on Windows."""
8793
if isinstance(s, str):
@@ -93,7 +99,8 @@ def win_encode(s: Optional[AnyStr]) -> Optional[bytes]:
9399
return None
94100

95101

96-
def with_metaclass(meta: Type[Any], *bases: Any) -> TBD: # type: ignore ## mypy cannot understand dynamic class creation
102+
# type: ignore ## mypy cannot understand dynamic class creation
103+
def with_metaclass(meta: Type[Any], *bases: Any) -> TBD:
97104
"""copied from https://github.com/Byron/bcore/blob/master/src/python/butility/future.py#L15"""
98105

99106
class metaclass(meta): # type: ignore
@@ -105,4 +112,4 @@ def __new__(cls, name: str, nbases: Optional[Tuple[int, ...]], d: Dict[str, Any]
105112
return type.__new__(cls, name, (), d)
106113
return meta(name, bases, d)
107114

108-
return metaclass(meta.__name__ + 'Helper', None, {}) # type: ignore
115+
return metaclass(meta.__name__ + 'Helper', None, {}) # type: ignore

‎git/config.py

Lines changed: 109 additions & 81 deletions
Large diffs are not rendered by default.

‎git/diff.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from .objects.tree import Tree
2323
from git.repo.base import Repo
2424

25+
from subprocess import Popen
26+
2527
Lit_change_type = Literal['A', 'D', 'M', 'R', 'T']
2628

2729
# ------------------------------------------------------------------------
@@ -490,7 +492,7 @@ def _index_from_patch_format(cls, repo: 'Repo', proc: TBD) -> DiffIndex:
490492
return index
491493

492494
@staticmethod
493-
def _handle_diff_line(lines_bytes: bytes, repo: 'Repo', index: TBD) -> None:
495+
def _handle_diff_line(lines_bytes: bytes, repo: 'Repo', index: DiffIndex) -> None:
494496
lines = lines_bytes.decode(defenc)
495497

496498
for line in lines.split(':')[1:]:
@@ -542,14 +544,14 @@ def _handle_diff_line(lines_bytes: bytes, repo: 'Repo', index: TBD) -> None:
542544
index.append(diff)
543545

544546
@classmethod
545-
def _index_from_raw_format(cls, repo: 'Repo', proc: TBD) -> DiffIndex:
547+
def _index_from_raw_format(cls, repo: 'Repo', proc: 'Popen') -> 'DiffIndex':
546548
"""Create a new DiffIndex from the given stream which must be in raw format.
547549
:return: git.DiffIndex"""
548550
# handles
549551
# :100644 100644 687099101... 37c5e30c8... M .gitignore
550552

551553
index = DiffIndex()
552-
handle_process_output(proc, lambda bytes: cls._handle_diff_line(
553-
bytes, repo, index), None, finalize_process, decode_streams=False)
554+
handle_process_output(proc, lambda byt: cls._handle_diff_line(byt, repo, index),
555+
None, finalize_process, decode_streams=False)
554556

555557
return index

‎git/exc.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
# typing ----------------------------------------------------
1313

14-
from typing import IO, List, Optional, Tuple, Union, TYPE_CHECKING
14+
from typing import List, Optional, Tuple, Union, TYPE_CHECKING
1515
from git.types import PathLike
1616

1717
if TYPE_CHECKING:
@@ -49,8 +49,9 @@ class CommandError(GitError):
4949
_msg = "Cmd('%s') failed%s"
5050

5151
def __init__(self, command: Union[List[str], Tuple[str, ...], str],
52-
status: Union[str, None, Exception] = None,
53-
stderr: Optional[IO[str]] = None, stdout: Optional[IO[str]] = None) -> None:
52+
status: Union[str, int, None, Exception] = None,
53+
stderr: Union[bytes, str, None] = None,
54+
stdout: Union[bytes, str, None] = None) -> None:
5455
if not isinstance(command, (tuple, list)):
5556
command = command.split()
5657
self.command = command
@@ -91,9 +92,9 @@ class GitCommandError(CommandError):
9192
""" Thrown if execution of the git command fails with non-zero status code. """
9293

9394
def __init__(self, command: Union[List[str], Tuple[str, ...], str],
94-
status: Union[str, None, Exception] = None,
95-
stderr: Optional[IO[str]] = None,
96-
stdout: Optional[IO[str]] = None,
95+
status: Union[str, int, None, Exception] = None,
96+
stderr: Union[bytes, str, None] = None,
97+
stdout: Union[bytes, str, None] = None,
9798
) -> None:
9899
super(GitCommandError, self).__init__(command, status, stderr, stdout)
99100

@@ -139,7 +140,7 @@ class HookExecutionError(CommandError):
139140
via standard output"""
140141

141142
def __init__(self, command: Union[List[str], Tuple[str, ...], str], status: Optional[str],
142-
stderr: Optional[IO[str]] = None, stdout: Optional[IO[str]] = None) -> None:
143+
stderr: Optional[str] = None, stdout: Optional[str] = None) -> None:
143144
super(HookExecutionError, self).__init__(command, status, stderr, stdout)
144145
self._msg = "Hook('%s') failed%s"
145146

‎git/repo/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
# typing ------------------------------------------------------
3636

37-
from git.types import TBD, PathLike, Literal
37+
from git.types import TBD, PathLike, Lit_config_levels
3838
from typing import (Any, BinaryIO, Callable, Dict,
3939
Iterator, List, Mapping, Optional,
4040
TextIO, Tuple, Type, Union,
@@ -45,7 +45,6 @@
4545
from git.refs.symbolic import SymbolicReference
4646
from git.objects import TagObject, Blob, Tree # NOQA: F401
4747

48-
Lit_config_levels = Literal['system', 'global', 'user', 'repository']
4948

5049
# -----------------------------------------------------------
5150

‎git/types.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
from typing_extensions import Final, Literal # noqa: F401
1313

1414

15-
TBD = Any
16-
1715
if sys.version_info[:2] < (3, 6):
1816
# os.PathLike (PEP-519) only got introduced with Python 3.6
1917
PathLike = str
@@ -22,4 +20,8 @@
2220
PathLike = Union[str, os.PathLike]
2321
elif sys.version_info[:2] >= (3, 9):
2422
# os.PathLike only becomes subscriptable from Python 3.9 onwards
25-
PathLike = Union[str, os.PathLike[str]]
23+
PathLike = Union[str, 'os.PathLike[str]'] # forward ref as pylance complains unless editing with py3.9+
24+
25+
TBD = Any
26+
27+
Lit_config_levels = Literal['system', 'global', 'user', 'repository']

‎git/util.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
# typing ---------------------------------------------------------
2323

2424
from typing import (Any, AnyStr, BinaryIO, Callable, Dict, Generator, IO, Iterator, List,
25-
Optional, Pattern, Sequence, Tuple, Union, cast, TYPE_CHECKING)
25+
Optional, Pattern, Sequence, Tuple, Union, cast, TYPE_CHECKING, overload)
26+
27+
2628
if TYPE_CHECKING:
2729
from git.remote import Remote
2830
from git.repo.base import Repo
29-
from .types import PathLike, TBD
31+
from .types import PathLike, TBD, Literal
3032

3133
# ---------------------------------------------------------------------
3234

@@ -281,7 +283,8 @@ def _cygexpath(drive: Optional[str], path: PathLike) -> str:
281283

282284
def cygpath(path: PathLike) -> PathLike:
283285
"""Use :meth:`git.cmd.Git.polish_url()` instead, that works on any environment."""
284-
path = str(path) # ensure is str and not AnyPath
286+
path = str(path) # ensure is str and not AnyPath.
287+
#Fix to use Paths when 3.5 dropped. or to be just str if only for urls?
285288
if not path.startswith(('/cygdrive', '//')):
286289
for regex, parser, recurse in _cygpath_parsers:
287290
match = regex.match(path)
@@ -314,11 +317,23 @@ def decygpath(path: PathLike) -> str:
314317
_is_cygwin_cache = {} # type: Dict[str, Optional[bool]]
315318

316319

320+
@overload
321+
def is_cygwin_git(git_executable: None) -> Literal[False]:
322+
...
323+
324+
325+
@overload
317326
def is_cygwin_git(git_executable: PathLike) -> bool:
327+
...
328+
329+
330+
def is_cygwin_git(git_executable: Union[None, PathLike]) -> bool:
318331
if not is_win:
319332
return False
320333

321-
#from subprocess import check_output
334+
if git_executable is None:
335+
return False
336+
322337
git_executable = str(git_executable)
323338
is_cygwin = _is_cygwin_cache.get(git_executable) # type: Optional[bool]
324339
if is_cygwin is None:
@@ -348,18 +363,31 @@ def get_user_id() -> str:
348363
return "%s@%s" % (getpass.getuser(), platform.node())
349364

350365

351-
def finalize_process(proc: TBD, **kwargs: Any) -> None:
366+
def finalize_process(proc: subprocess.Popen, **kwargs: Any) -> None:
352367
"""Wait for the process (clone, fetch, pull or push) and handle its errors accordingly"""
353368
## TODO: No close proc-streams??
354369
proc.wait(**kwargs)
355370

356371

357-
def expand_path(p: PathLike, expand_vars: bool = True) -> Optional[PathLike]:
372+
@overload
373+
def expand_path(p: None, expand_vars: bool = ...) -> None:
374+
...
375+
376+
377+
@overload
378+
def expand_path(p: PathLike, expand_vars: bool = ...) -> str:
379+
...
380+
381+
382+
def expand_path(p: Union[None, PathLike], expand_vars: bool = True) -> Optional[str]:
358383
try:
359-
p = osp.expanduser(p)
360-
if expand_vars:
361-
p = osp.expandvars(p)
362-
return osp.normpath(osp.abspath(p))
384+
if p is not None:
385+
p_out = osp.expanduser(p)
386+
if expand_vars:
387+
p_out = osp.expandvars(p_out)
388+
return osp.normpath(osp.abspath(p_out))
389+
else:
390+
return None
363391
except Exception:
364392
return None
365393

0 commit comments

Comments
 (0)
Please sign in to comment.