Skip to content

Add types to git/config.py #1234

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 14 commits into from
May 13, 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
flake8 and mypy fixes
  • Loading branch information
Yobmod committed May 13, 2021
commit 96c43652c9f5b11b611e1aca0a6d67393e9e38c1
40 changes: 20 additions & 20 deletions git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# typing ---------------------------------------------------------------------------

from typing import (Any, AnyStr, BinaryIO, Callable, Dict, IO, List, Mapping,
Sequence, TYPE_CHECKING, Tuple, Union, cast, overload)
Sequence, TYPE_CHECKING, TextIO, Tuple, Union, cast, overload)

from git.types import PathLike, Literal, TBD

Expand Down Expand Up @@ -98,14 +98,17 @@ def handle_process_output(process: subprocess.Popen,
or if decoding must happen later (i.e. for Diffs).
"""
# Use 2 "pump" threads and wait for both to finish.
def pump_stream(cmdline: str, name: str, stream: BinaryIO, is_decode: bool,
handler: Union[None, Callable[[str], None]]) -> None:
def pump_stream(cmdline: str, name: str, stream: Union[BinaryIO, TextIO], is_decode: bool,
handler: Union[None, Callable[[Union[bytes, str]], None]]) -> None:
try:
for line in stream:
if handler:
if is_decode:
assert isinstance(line, bytes)
line_str = line.decode(defenc)
handler(line_str)
handler(line_str)
else:
handler(line)
except Exception as ex:
log.error("Pumping %r of cmd(%s) failed due to: %r", name, remove_password_if_present(cmdline), ex)
raise CommandError(['<%s-pump>' % name] + remove_password_if_present(cmdline), ex) from ex
Expand Down Expand Up @@ -337,12 +340,12 @@ def is_cygwin(cls) -> bool:

@overload
@classmethod
def polish_url(cls, url: str, is_cygwin: Union[None, bool] = None) -> str:
def polish_url(cls, url: str, is_cygwin: Literal[False] = ...) -> str:
...

@overload
@classmethod
def polish_url(cls, url: PathLike, is_cygwin: Union[None, bool] = None) -> PathLike:
def polish_url(cls, url: PathLike, is_cygwin: Union[None, bool] = None) -> str:
...

@classmethod
Expand Down Expand Up @@ -628,16 +631,16 @@ def version_info(self) -> Tuple[int, int, int, int]:
def execute(self,
command: Union[str, Sequence[Any]],
*,
as_process: Literal[True],
) -> AutoInterrupt:
as_process: Literal[True]
) -> 'AutoInterrupt':
...

@overload
def execute(self,
command: Union[str, Sequence[Any]],
*,
as_process: Literal[False] = False,
stdout_as_string: Literal[True],
stdout_as_string: Literal[True]
) -> Union[str, Tuple[int, str, str]]:
...

Expand All @@ -646,7 +649,7 @@ def execute(self,
command: Union[str, Sequence[Any]],
*,
as_process: Literal[False] = False,
stdout_as_string: Literal[False] = False,
stdout_as_string: Literal[False] = False
) -> Union[bytes, Tuple[int, bytes, str]]:
...

Expand All @@ -656,8 +659,7 @@ def execute(self,
*,
with_extended_output: Literal[False],
as_process: Literal[False],
stdout_as_string: Literal[True],

stdout_as_string: Literal[True]
) -> str:
...

Expand All @@ -667,8 +669,7 @@ def execute(self,
*,
with_extended_output: Literal[False],
as_process: Literal[False],
stdout_as_string: Literal[False],

stdout_as_string: Literal[False]
) -> bytes:
...

Expand Down Expand Up @@ -829,16 +830,13 @@ def execute(self,
creationflags=PROC_CREATIONFLAGS,
**subprocess_kwargs
)
proc = cast(Popen[bytes], proc)

proc.stdout = cast(BinaryIO, proc.stdout)
except cmd_not_found_exception as err:
raise GitCommandNotFound(redacted_command, err) from err
else:
assert isinstance(proc.stdout, BinaryIO)
assert isinstance(proc.stderr, BinaryIO)
# proc.stdout = cast(BinaryIO, proc.stdout)
# proc.stderr = cast(BinaryIO, proc.stderr)
proc = cast(Popen, proc)
proc.stdout = cast(BinaryIO, proc.stdout)
proc.stderr = cast(BinaryIO, proc.stderr)

if as_process:
return self.AutoInterrupt(proc, command)
Expand Down Expand Up @@ -1164,6 +1162,8 @@ def _prepare_ref(self, ref: AnyStr) -> bytes:
refstr = ref.decode('ascii') # type: str
elif not isinstance(ref, str):
refstr = str(ref) # could be ref-object
else:
refstr = ref

if not refstr.endswith("\n"):
refstr += "\n"
Expand Down
10 changes: 0 additions & 10 deletions git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,6 @@ def _cygexpath(drive: Optional[str], path: PathLike) -> str:
) # type: Tuple[Tuple[Pattern[str], Callable, bool], ...]


@overload
def cygpath(path: str) -> str:
...


@overload
def cygpath(path: PathLike) -> PathLike:
...


def cygpath(path: PathLike) -> PathLike:
"""Use :meth:`git.cmd.Git.polish_url()` instead, that works on any environment."""
path = str(path) # ensure is str and not AnyPath.
Expand Down