Skip to content

Adding inital types to remote.py #1229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
start add types to util.py
  • Loading branch information
Yobmod committed Feb 28, 2021
commit 5b0028e1e75e1ee0eea63ba78cb3160d49c1f3a3
13 changes: 8 additions & 5 deletions git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import threading
from collections import OrderedDict
from textwrap import dedent
from typing import Any, Dict, List, Optional

from git.compat import (
defenc,
Expand All @@ -39,6 +40,8 @@
stream_copy,
)

from .types import PathLike

execute_kwargs = {'istream', 'with_extended_output',
'with_exceptions', 'as_process', 'stdout_as_string',
'output_stream', 'with_stdout', 'kill_after_timeout',
Expand Down Expand Up @@ -516,7 +519,7 @@ def __del__(self):
self._stream.read(bytes_left + 1)
# END handle incomplete read

def __init__(self, working_dir=None):
def __init__(self, working_dir: Optional[PathLike]=None) -> None:
"""Initialize this instance with:

:param working_dir:
Expand All @@ -525,12 +528,12 @@ def __init__(self, working_dir=None):
It is meant to be the working tree directory if available, or the
.git directory in case of bare repositories."""
super(Git, self).__init__()
self._working_dir = expand_path(working_dir)
self._working_dir = expand_path(working_dir) if working_dir is not None else None
self._git_options = ()
self._persistent_git_options = []
self._persistent_git_options = [] # type: List[str]

# Extra environment variables to pass to git commands
self._environment = {}
self._environment = {} # type: Dict[str, Any]

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

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

Expand Down
21 changes: 14 additions & 7 deletions git/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import locale
import os
import sys
from typing import AnyStr, Optional, Type


from gitdb.utils.encoding import (
Expand All @@ -18,40 +19,46 @@
)


is_win = (os.name == 'nt')
is_win = (os.name == 'nt') # type: bool
is_posix = (os.name == 'posix')
is_darwin = (os.name == 'darwin')
defenc = sys.getfilesystemencoding()


def safe_decode(s):
def safe_decode(s: Optional[AnyStr]) -> Optional[str]:
"""Safely decodes a binary string to unicode"""
if isinstance(s, str):
return s
elif isinstance(s, bytes):
return s.decode(defenc, 'surrogateescape')
elif s is not None:
elif s is None:
return None
else:
raise TypeError('Expected bytes or text, but got %r' % (s,))


def safe_encode(s):
"""Safely decodes a binary string to unicode"""

def safe_encode(s: Optional[AnyStr]) -> Optional[bytes]:
"""Safely encodes a binary string to unicode"""
if isinstance(s, str):
return s.encode(defenc)
elif isinstance(s, bytes):
return s
elif s is not None:
elif s is None:
return None
else:
raise TypeError('Expected bytes or text, but got %r' % (s,))


def win_encode(s):
def win_encode(s: Optional[AnyStr]) -> Optional[bytes]:
"""Encode unicodes for process arguments on Windows."""
if isinstance(s, str):
return s.encode(locale.getpreferredencoding(False))
elif isinstance(s, bytes):
return s
elif s is not None:
raise TypeError('Expected bytes or text, but got %r' % (s,))
return None


def with_metaclass(meta, *bases):
Expand Down
4 changes: 3 additions & 1 deletion git/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import fnmatch
from collections import OrderedDict

from typing_extensions import Literal

from git.compat import (
defenc,
force_text,
Expand Down Expand Up @@ -194,7 +196,7 @@ def items_all(self):
return [(k, self.getall(k)) for k in self]


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

# we do not support an absolute path of the gitconfig on windows ,
# use the global config instead
Expand Down
2 changes: 1 addition & 1 deletion git/refs/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None):
return ref

@classmethod
def create(cls, repo, path, reference='HEAD', force=False, logmsg=None):
def create(cls, repo, path, reference='HEAD', force=False, logmsg=None, **kwargs):
"""Create a new symbolic reference, hence a reference pointing to another reference.

:param repo:
Expand Down
Loading