Skip to content

Commit

Permalink
Merge pull request #6704 from cjerdonek/mypy-per-file-strict-optional
Browse files Browse the repository at this point in the history
Use mypy's default of strict_optional=True
  • Loading branch information
cjerdonek authored Jul 15, 2019
2 parents 2c36f4d + 190ea6a commit 90905fc
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 22 deletions.
71 changes: 71 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,82 @@ ignore = W504
[mypy]
follow_imports = silent
ignore_missing_imports = True

[mypy-pip/_internal/build_env]
strict_optional = False

[mypy-pip/_internal/cache]
strict_optional = False

[mypy-pip/_internal/cli/base_command]
strict_optional = False

[mypy-pip/_internal/cli/cmdoptions]
strict_optional = False

[mypy-pip/_internal/configuration]
strict_optional = False

[mypy-pip/_internal/index]
strict_optional = False

[mypy-pip/_internal/legacy_resolve]
strict_optional = False

[mypy-pip/_internal/locations]
strict_optional = False

[mypy-pip/_internal/models/format_control]
strict_optional = False

[mypy-pip/_internal/operations/check]
strict_optional = False

[mypy-pip/_internal/operations/freeze]
strict_optional = False

[mypy-pip/_internal/operations/prepare]
strict_optional = False

[mypy-pip/_internal/pep425tags]
strict_optional = False

[mypy-pip/_internal/req/*]
disallow_untyped_defs = True

[mypy-pip/_internal/req]
strict_optional = False

[mypy-pip/_internal/req/constructors]
strict_optional = False

[mypy-pip/_internal/req/req_file]
strict_optional = False

[mypy-pip/_internal/req/req_install]
strict_optional = False

[mypy-pip/_internal/req/req_set]
strict_optional = False

[mypy-pip/_internal/req/req_tracker]
strict_optional = False

[mypy-pip/_internal/utils/encoding]
strict_optional = False

[mypy-pip/_internal/utils/glibc]
strict_optional = False

[mypy-pip/_internal/utils/misc]
strict_optional = False

[mypy-pip/_internal/utils/ui]
strict_optional = False

[mypy-pip/_internal/wheel]
strict_optional = False

[mypy-pip/_vendor/*]
follow_imports = skip
ignore_errors = True
Expand Down
17 changes: 10 additions & 7 deletions src/pip/_internal/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
from pip._internal.utils.hashes import Hashes
from pip._internal.vcs.versioncontrol import AuthInfo, VersionControl

Credentials = Tuple[str, str, str]


__all__ = ['get_file_content',
'is_url', 'url_to_path', 'path_to_url',
'is_archive_file', 'unpack_vcs_link',
Expand Down Expand Up @@ -224,7 +227,7 @@ def __init__(self, prompting=True, index_urls=None):
# this value is set to the credentials they entered. After the
# request authenticates, the caller should call
# ``save_credentials`` to save these.
self._credentials_to_save = None # type: Tuple[str, str, str]
self._credentials_to_save = None # type: Optional[Credentials]

def _get_index_url(self, url):
"""Return the original index URL matching the requested URL.
Expand Down Expand Up @@ -748,7 +751,7 @@ def _download_url(
resp, # type: Response
link, # type: Link
content_file, # type: IO
hashes, # type: Hashes
hashes, # type: Optional[Hashes]
progress_bar # type: str
):
# type: (...) -> None
Expand Down Expand Up @@ -1002,8 +1005,8 @@ def request(self, host, handler, request_body, verbose=False):


def unpack_url(
link, # type: Optional[Link]
location, # type: Optional[str]
link, # type: Link
location, # type: str
download_dir=None, # type: Optional[str]
only_download=False, # type: bool
session=None, # type: Optional[PipSession]
Expand Down Expand Up @@ -1077,7 +1080,7 @@ def _download_http_url(
link, # type: Link
session, # type: PipSession
temp_dir, # type: str
hashes, # type: Hashes
hashes, # type: Optional[Hashes]
progress_bar # type: str
):
# type: (...) -> Tuple[str, str]
Expand Down Expand Up @@ -1121,7 +1124,7 @@ def _download_http_url(
content_disposition = resp.headers.get('content-disposition')
if content_disposition:
filename = parse_content_disposition(content_disposition, filename)
ext = splitext(filename)[1]
ext = splitext(filename)[1] # type: Optional[str]
if not ext:
ext = mimetypes.guess_extension(content_type)
if ext:
Expand All @@ -1137,7 +1140,7 @@ def _download_http_url(


def _check_download_dir(link, download_dir, hashes):
# type: (Link, str, Hashes) -> Optional[str]
# type: (Link, str, Optional[Hashes]) -> Optional[str]
""" Check download_dir for previously downloaded file with correct hash
If a correct file is found return its path else None
"""
Expand Down
3 changes: 3 additions & 0 deletions src/pip/_internal/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,8 @@ def is_hash_allowed(self, hashes):
"""
if not self.has_hash:
return False
# Assert non-None so mypy knows self.hash_name and self.hash are str.
assert self.hash_name is not None
assert self.hash is not None

return hashes.is_hash_allowed(self.hash_name, hex_digest=self.hash)
10 changes: 5 additions & 5 deletions src/pip/_internal/req/req_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ def __init__(
self.req = req
self.comes_from = comes_from
self.constraint = constraint
if source_dir is not None:
self.source_dir = os.path.normpath(os.path.abspath(source_dir))
if source_dir is None:
self.source_dir = None # type: Optional[str]
else:
self.source_dir = None
self.source_dir = os.path.normpath(os.path.abspath(source_dir))
self.editable = editable

self._wheel_cache = wheel_cache
Expand Down Expand Up @@ -169,7 +169,7 @@ def __str__(self):
s += ' in %s' % display_path(self.satisfied_by.location)
if self.comes_from:
if isinstance(self.comes_from, six.string_types):
comes_from = self.comes_from
comes_from = self.comes_from # type: Optional[str]
else:
comes_from = self.comes_from.from_path()
if comes_from:
Expand Down Expand Up @@ -294,7 +294,7 @@ def from_path(self):
return s

def build_location(self, build_dir):
# type: (str) -> Optional[str]
# type: (str) -> str
assert build_dir is not None
if self._temp_build_dir.path is not None:
return self._temp_build_dir.path
Expand Down
26 changes: 23 additions & 3 deletions src/pip/_internal/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,31 @@


try:
from pip._vendor import colorama
# Use "import as" and set colorama in the else clause to avoid mypy
# errors and get the following correct revealed type for colorama:
# `Union[_importlib_modulespec.ModuleType, None]`
# Otherwise, we get an error like the following in the except block:
# > Incompatible types in assignment (expression has type "None",
# variable has type Module)
# TODO: eliminate the need to use "import as" once mypy addresses some
# of its issues with conditional imports. Here is an umbrella issue:
# https://github.com/python/mypy/issues/1297
from pip._vendor import colorama as _colorama
# Lots of different errors can come from this, including SystemError and
# ImportError.
except Exception:
colorama = None
else:
# Import Fore explicitly rather than accessing below as colorama.Fore
# to avoid the following error running mypy:
# > Module has no attribute "Fore"
# TODO: eliminate the need to import Fore once mypy addresses some of its
# issues with conditional imports. This particular case could be an
# instance of the following issue (but also see the umbrella issue above):
# https://github.com/python/mypy/issues/3500
from pip._vendor.colorama import Fore

colorama = _colorama


_log_state = threading.local()
Expand Down Expand Up @@ -153,8 +173,8 @@ class ColorizedStreamHandler(logging.StreamHandler):
if colorama:
COLORS = [
# This needs to be in order from highest logging level to lowest.
(logging.ERROR, _color_wrap(colorama.Fore.RED)),
(logging.WARNING, _color_wrap(colorama.Fore.YELLOW)),
(logging.ERROR, _color_wrap(Fore.RED)),
(logging.WARNING, _color_wrap(Fore.YELLOW)),
]
else:
COLORS = []
Expand Down
7 changes: 2 additions & 5 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def get_pip_version():


def normalize_version_info(py_version_info):
# type: (Optional[Tuple[int, ...]]) -> Optional[Tuple[int, int, int]]
# type: (Tuple[int, ...]) -> Tuple[int, int, int]
"""
Convert a tuple of ints representing a Python version to one of length
three.
Expand All @@ -129,9 +129,6 @@ def normalize_version_info(py_version_info):
:return: a tuple of length three if `py_version_info` is non-None.
Otherwise, return `py_version_info` unchanged (i.e. None).
"""
if py_version_info is None:
return None

if len(py_version_info) < 3:
py_version_info += (3 - len(py_version_info)) * (0,)
elif len(py_version_info) > 3:
Expand Down Expand Up @@ -824,7 +821,7 @@ def call_subprocess(
unset_environ=None, # type: Optional[Iterable[str]]
spinner=None # type: Optional[SpinnerInterface]
):
# type: (...) -> Optional[Text]
# type: (...) -> Text
"""
Args:
show_stdout: if true, use INFO to log the subprocess's stderr and
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/vcs/versioncontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def run_command(
extra_environ=None, # type: Optional[Mapping[str, Any]]
spinner=None # type: Optional[SpinnerInterface]
):
# type: (...) -> Optional[Text]
# type: (...) -> Text
"""
Run a VCS subcommand
This is simply a wrapper around call_subprocess that adds the VCS
Expand Down
1 change: 0 additions & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,6 @@ def test_manylinux_check_glibc_version(self):


@pytest.mark.parametrize('version_info, expected', [
(None, None),
((), (0, 0, 0)),
((3, ), (3, 0, 0)),
((3, 6), (3, 6, 0)),
Expand Down

0 comments on commit 90905fc

Please sign in to comment.