Skip to content

Commit 5b0028e

Browse files
committed
start add types to util.py
1 parent ad4079d commit 5b0028e

File tree

5 files changed

+135
-97
lines changed

5 files changed

+135
-97
lines changed

Diff for: git/cmd.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import threading
2020
from collections import OrderedDict
2121
from textwrap import dedent
22+
from typing import Any, Dict, List, Optional
2223

2324
from git.compat import (
2425
defenc,
@@ -39,6 +40,8 @@
3940
stream_copy,
4041
)
4142

43+
from .types import PathLike
44+
4245
execute_kwargs = {'istream', 'with_extended_output',
4346
'with_exceptions', 'as_process', 'stdout_as_string',
4447
'output_stream', 'with_stdout', 'kill_after_timeout',
@@ -516,7 +519,7 @@ def __del__(self):
516519
self._stream.read(bytes_left + 1)
517520
# END handle incomplete read
518521

519-
def __init__(self, working_dir=None):
522+
def __init__(self, working_dir: Optional[PathLike]=None) -> None:
520523
"""Initialize this instance with:
521524
522525
:param working_dir:
@@ -525,12 +528,12 @@ def __init__(self, working_dir=None):
525528
It is meant to be the working tree directory if available, or the
526529
.git directory in case of bare repositories."""
527530
super(Git, self).__init__()
528-
self._working_dir = expand_path(working_dir)
531+
self._working_dir = expand_path(working_dir) if working_dir is not None else None
529532
self._git_options = ()
530-
self._persistent_git_options = []
533+
self._persistent_git_options = [] # type: List[str]
531534

532535
# Extra environment variables to pass to git commands
533-
self._environment = {}
536+
self._environment = {} # type: Dict[str, Any]
534537

535538
# cached command slots
536539
self.cat_file_header = None
@@ -544,7 +547,7 @@ def __getattr__(self, name):
544547
return LazyMixin.__getattr__(self, name)
545548
return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
546549

547-
def set_persistent_git_options(self, **kwargs):
550+
def set_persistent_git_options(self, **kwargs) -> None:
548551
"""Specify command line options to the git executable
549552
for subsequent subcommand calls
550553

Diff for: git/compat.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import locale
1111
import os
1212
import sys
13+
from typing import AnyStr, Optional, Type
1314

1415

1516
from gitdb.utils.encoding import (
@@ -18,40 +19,46 @@
1819
)
1920

2021

21-
is_win = (os.name == 'nt')
22+
is_win = (os.name == 'nt') # type: bool
2223
is_posix = (os.name == 'posix')
2324
is_darwin = (os.name == 'darwin')
2425
defenc = sys.getfilesystemencoding()
2526

2627

27-
def safe_decode(s):
28+
def safe_decode(s: Optional[AnyStr]) -> Optional[str]:
2829
"""Safely decodes a binary string to unicode"""
2930
if isinstance(s, str):
3031
return s
3132
elif isinstance(s, bytes):
3233
return s.decode(defenc, 'surrogateescape')
33-
elif s is not None:
34+
elif s is None:
35+
return None
36+
else:
3437
raise TypeError('Expected bytes or text, but got %r' % (s,))
3538

3639

37-
def safe_encode(s):
38-
"""Safely decodes a binary string to unicode"""
40+
41+
def safe_encode(s: Optional[AnyStr]) -> Optional[bytes]:
42+
"""Safely encodes a binary string to unicode"""
3943
if isinstance(s, str):
4044
return s.encode(defenc)
4145
elif isinstance(s, bytes):
4246
return s
43-
elif s is not None:
47+
elif s is None:
48+
return None
49+
else:
4450
raise TypeError('Expected bytes or text, but got %r' % (s,))
4551

4652

47-
def win_encode(s):
53+
def win_encode(s: Optional[AnyStr]) -> Optional[bytes]:
4854
"""Encode unicodes for process arguments on Windows."""
4955
if isinstance(s, str):
5056
return s.encode(locale.getpreferredencoding(False))
5157
elif isinstance(s, bytes):
5258
return s
5359
elif s is not None:
5460
raise TypeError('Expected bytes or text, but got %r' % (s,))
61+
return None
5562

5663

5764
def with_metaclass(meta, *bases):

Diff for: git/config.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import fnmatch
1717
from collections import OrderedDict
1818

19+
from typing_extensions import Literal
20+
1921
from git.compat import (
2022
defenc,
2123
force_text,
@@ -194,7 +196,7 @@ def items_all(self):
194196
return [(k, self.getall(k)) for k in self]
195197

196198

197-
def get_config_path(config_level):
199+
def get_config_path(config_level: Literal['system','global','user','repository']) -> str:
198200

199201
# we do not support an absolute path of the gitconfig on windows ,
200202
# use the global config instead

Diff for: git/refs/symbolic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None):
513513
return ref
514514

515515
@classmethod
516-
def create(cls, repo, path, reference='HEAD', force=False, logmsg=None):
516+
def create(cls, repo, path, reference='HEAD', force=False, logmsg=None, **kwargs):
517517
"""Create a new symbolic reference, hence a reference pointing to another reference.
518518
519519
:param repo:

0 commit comments

Comments
 (0)