Skip to content
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

Add mypy annotations to pip._internal.compat #5945

Merged
merged 1 commit into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
Empty file.
28 changes: 22 additions & 6 deletions src/pip/_internal/utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

from pip._vendor.six import text_type

from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from typing import Tuple, Text # noqa: F401

try:
import ipaddress
except ImportError:
Expand Down Expand Up @@ -68,6 +73,7 @@ def backslashreplace_decode_fn(err):


def console_to_str(data):
# type: (bytes) -> Text
"""Return a string, safe for output, of subprocess output.

We assume the data is in the locale preferred encoding.
Expand All @@ -88,13 +94,13 @@ def console_to_str(data):
# Now try to decode the data - if we fail, warn the user and
# decode with replacement.
try:
s = data.decode(encoding)
decoded_data = data.decode(encoding)
except UnicodeDecodeError:
logger.warning(
"Subprocess output does not appear to be encoded as %s",
encoding,
)
s = data.decode(encoding, errors=backslashreplace_decode)
decoded_data = data.decode(encoding, errors=backslashreplace_decode)

# Make sure we can print the output, by encoding it to the output
# encoding with replacement of unencodable characters, and then
Expand All @@ -112,27 +118,33 @@ def console_to_str(data):
"encoding", None)

if output_encoding:
s = s.encode(output_encoding, errors="backslashreplace")
s = s.decode(output_encoding)
output_encoded = decoded_data.encode(
output_encoding,
errors="backslashreplace"
)
decoded_data = output_encoded.decode(output_encoding)

return s
return decoded_data


if sys.version_info >= (3,):
def native_str(s, replace=False):
# type: (str, bool) -> str
if isinstance(s, bytes):
return s.decode('utf-8', 'replace' if replace else 'strict')
return s

else:
def native_str(s, replace=False):
# type: (str, bool) -> str
# Replace is ignored -- unicode to UTF-8 can't fail
if isinstance(s, text_type):
return s.encode('utf-8')
return s


def get_path_uid(path):
# type: (str) -> int
"""
Return path's uid.

Expand Down Expand Up @@ -174,6 +186,7 @@ def get_extension_suffixes():


def expanduser(path):
# type: (str) -> str
"""
Expand ~ and ~user constructions.

Expand All @@ -199,6 +212,7 @@ def expanduser(path):


def samefile(file1, file2):
# type: (str, str) -> bool
"""Provide an alternative for os.path.samefile on Windows/Python2"""
if hasattr(os.path, 'samefile'):
return os.path.samefile(file1, file2)
Expand All @@ -210,13 +224,15 @@ def samefile(file1, file2):

if hasattr(shutil, 'get_terminal_size'):
def get_terminal_size():
# type: () -> Tuple[int, int]
"""
Returns a tuple (x, y) representing the width(x) and the height(y)
in characters of the terminal window.
"""
return tuple(shutil.get_terminal_size())
return tuple(shutil.get_terminal_size()) # type: ignore
else:
def get_terminal_size():
# type: () -> Tuple[int, int]
"""
Returns a tuple (x, y) representing the width(x) and the height(y)
in characters of the terminal window.
Expand Down