Skip to content

ci: check types with mypy #1226

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 9 commits into from
Apr 23, 2021
Prev Previous commit
Next Next commit
fix(mypy): properly describe link between parameter and return types
This gives mypy all information that it needs to determine what the
return type of a function call is *iff* it knows the argument's type.

As a result it can now stop complaining about passing None to str.join()
in exc.py.
  • Loading branch information
muggenhor committed Apr 23, 2021
commit 6a233359ce1ec30386f97d4acdf989f1c3570842
30 changes: 28 additions & 2 deletions git/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@

# typing --------------------------------------------------------------------

from typing import IO, Any, AnyStr, Dict, Optional, Type, Union
from typing import (
Any,
AnyStr,
Dict,
IO,
Optional,
Type,
Union,
overload,
)
from git.types import TBD

# ---------------------------------------------------------------------------
Expand All @@ -30,6 +39,12 @@
defenc = sys.getfilesystemencoding()


@overload
def safe_decode(s: None) -> None: ...

@overload
def safe_decode(s: Union[IO[str], AnyStr]) -> str: ...

def safe_decode(s: Union[IO[str], AnyStr, None]) -> Optional[str]:
"""Safely decodes a binary string to unicode"""
if isinstance(s, str):
Expand All @@ -42,6 +57,12 @@ def safe_decode(s: Union[IO[str], AnyStr, None]) -> Optional[str]:
raise TypeError('Expected bytes or text, but got %r' % (s,))


@overload
def safe_encode(s: None) -> None: ...

@overload
def safe_encode(s: AnyStr) -> bytes: ...

def safe_encode(s: Optional[AnyStr]) -> Optional[bytes]:
"""Safely encodes a binary string to unicode"""
if isinstance(s, str):
Expand All @@ -54,6 +75,12 @@ def safe_encode(s: Optional[AnyStr]) -> Optional[bytes]:
raise TypeError('Expected bytes or text, but got %r' % (s,))


@overload
def win_encode(s: None) -> None: ...

@overload
def win_encode(s: AnyStr) -> bytes: ...

def win_encode(s: Optional[AnyStr]) -> Optional[bytes]:
"""Encode unicodes for process arguments on Windows."""
if isinstance(s, str):
Expand All @@ -65,7 +92,6 @@ def win_encode(s: Optional[AnyStr]) -> Optional[bytes]:
return None



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

Expand Down