Type annotations for hash, show and wheel in commands

This commit is contained in:
Devesh Kumar Singh 2020-04-11 13:24:27 +05:30
parent 4416e88ef1
commit 0e8093ec5c
4 changed files with 33 additions and 16 deletions

View File

@ -1,6 +1,3 @@
# The following comment should be removed at some point in the future.
# mypy: disallow-untyped-defs=False
from __future__ import absolute_import
import hashlib
@ -8,9 +5,14 @@ import logging
import sys
from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import ERROR
from pip._internal.cli.status_codes import ERROR, SUCCESS
from pip._internal.utils.hashes import FAVORITE_HASH, STRONG_HASHES
from pip._internal.utils.misc import read_chunks, write_output
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING:
from optparse import Values
from typing import Any, List, Dict
logger = logging.getLogger(__name__)
@ -27,7 +29,9 @@ class HashCommand(Command):
ignore_require_venv = True
def __init__(self, *args, **kw):
super(HashCommand, self).__init__(*args, **kw)
# type: (List[Any], Dict[Any, Any]) -> None
# https://github.com/python/mypy/issues/4335
super(HashCommand, self).__init__(*args, **kw) # type: ignore
self.cmd_opts.add_option(
'-a', '--algorithm',
dest='algorithm',
@ -39,6 +43,7 @@ class HashCommand(Command):
self.parser.insert_option_group(0, self.cmd_opts)
def run(self, options, args):
# type: (Values, List[Any]) -> int
if not args:
self.parser.print_usage(sys.stderr)
return ERROR
@ -47,9 +52,11 @@ class HashCommand(Command):
for path in args:
write_output('%s:\n--hash=%s:%s',
path, algorithm, _hash_of_file(path, algorithm))
return SUCCESS
def _hash_of_file(path, algorithm):
# type: (str, str) -> str
"""Return the hash digest of a file."""
with open(path, 'rb') as archive:
hash = hashlib.new(algorithm)

View File

@ -1,6 +1,3 @@
# The following comment should be removed at some point in the future.
# mypy: disallow-untyped-defs=False
from __future__ import absolute_import
import logging
@ -13,6 +10,11 @@ from pip._vendor.packaging.utils import canonicalize_name
from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import ERROR, SUCCESS
from pip._internal.utils.misc import write_output
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING:
from optparse import Values
from typing import Any, List, Dict, Iterator
logger = logging.getLogger(__name__)
@ -29,7 +31,9 @@ class ShowCommand(Command):
ignore_require_venv = True
def __init__(self, *args, **kw):
super(ShowCommand, self).__init__(*args, **kw)
# type: (List[Any], Dict[Any, Any]) -> None
# https://github.com/python/mypy/issues/4335
super(ShowCommand, self).__init__(*args, **kw) # type: ignore
self.cmd_opts.add_option(
'-f', '--files',
dest='files',
@ -40,6 +44,7 @@ class ShowCommand(Command):
self.parser.insert_option_group(0, self.cmd_opts)
def run(self, options, args):
# type: (Values, List[Any]) -> int
if not args:
logger.warning('ERROR: Please provide a package name or names.')
return ERROR
@ -53,6 +58,7 @@ class ShowCommand(Command):
def search_packages_info(query):
# type: (List[Any]) -> Iterator[Dict[str, Any]]
"""
Gather details from installed distributions. Print distribution name,
version, location, and installed files. Installed files requires a
@ -71,6 +77,7 @@ def search_packages_info(query):
logger.warning('Package(s) not found: %s', ', '.join(missing))
def get_requiring_packages(package_name):
# type: (str) -> List[str]
canonical_name = canonicalize_name(package_name)
return [
pkg.project_name for pkg in pkg_resources.working_set
@ -88,7 +95,7 @@ def search_packages_info(query):
'required_by': get_requiring_packages(dist.project_name)
}
file_list = None
metadata = None
metadata = ''
if isinstance(dist, pkg_resources.DistInfoDistribution):
# RECORDs should be part of .dist-info metadatas
if dist.has_metadata('RECORD'):
@ -141,6 +148,7 @@ def search_packages_info(query):
def print_results(distributions, list_files=False, verbose=False):
# type: (Iterator[Dict[str, Any]], bool, bool) -> bool
"""
Print the information from installed distributions found.
"""

View File

@ -1,8 +1,5 @@
# -*- coding: utf-8 -*-
# The following comment should be removed at some point in the future.
# mypy: disallow-untyped-defs=False
from __future__ import absolute_import
import logging
@ -12,6 +9,7 @@ import shutil
from pip._internal.cache import WheelCache
from pip._internal.cli import cmdoptions
from pip._internal.cli.req_command import RequirementCommand, with_cleanup
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.exceptions import CommandError
from pip._internal.req.req_tracker import get_requirement_tracker
from pip._internal.utils.misc import ensure_dir, normalize_path
@ -21,7 +19,7 @@ from pip._internal.wheel_builder import build, should_build_for_wheel_command
if MYPY_CHECK_RUNNING:
from optparse import Values
from typing import Any, List
from typing import Any, List, Dict
logger = logging.getLogger(__name__)
@ -50,7 +48,9 @@ class WheelCommand(RequirementCommand):
%prog [options] <archive url/path> ..."""
def __init__(self, *args, **kw):
super(WheelCommand, self).__init__(*args, **kw)
# type: (List[Any], Dict[Any, Any]) -> None
# https://github.com/python/mypy/issues/4335
super(WheelCommand, self).__init__(*args, **kw) # type: ignore
cmd_opts = self.cmd_opts
@ -112,7 +112,7 @@ class WheelCommand(RequirementCommand):
@with_cleanup
def run(self, options, args):
# type: (Values, List[Any]) -> None
# type: (Values, List[Any]) -> int
cmdoptions.check_install_build_global(options)
session = self.get_default_session(options)
@ -188,3 +188,5 @@ class WheelCommand(RequirementCommand):
raise CommandError(
"Failed to build one or more wheels"
)
return SUCCESS