Skip to content

Type annotations for hash, show and wheel in commands #8018

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 3 commits into from
May 17, 2020
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.
14 changes: 10 additions & 4 deletions src/pip/_internal/commands/hash.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# The following comment should be removed at some point in the future.
# mypy: disallow-untyped-defs=False

from __future__ import absolute_import

import hashlib
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

logger = logging.getLogger(__name__)

Expand All @@ -27,6 +29,7 @@ class HashCommand(Command):
ignore_require_venv = True

def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
super(HashCommand, self).__init__(*args, **kw)
self.cmd_opts.add_option(
'-a', '--algorithm',
Expand All @@ -39,6 +42,7 @@ def __init__(self, *args, **kw):
self.parser.insert_option_group(0, self.cmd_opts)

def run(self, options, args):
# type: (Values, List[str]) -> int
if not args:
self.parser.print_usage(sys.stderr)
return ERROR
Expand All @@ -47,9 +51,11 @@ def run(self, options, args):
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)
Expand Down
15 changes: 11 additions & 4 deletions src/pip/_internal/commands/show.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +10,11 @@
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__)

Expand All @@ -29,6 +31,7 @@ class ShowCommand(Command):
ignore_require_venv = True

def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
super(ShowCommand, self).__init__(*args, **kw)
self.cmd_opts.add_option(
'-f', '--files',
Expand All @@ -40,6 +43,7 @@ def __init__(self, *args, **kw):
self.parser.insert_option_group(0, self.cmd_opts)

def run(self, options, args):
# type: (Values, List[str]) -> int
if not args:
logger.warning('ERROR: Please provide a package name or names.')
return ERROR
Expand All @@ -53,6 +57,7 @@ def run(self, options, args):


def search_packages_info(query):
# type: (List[str]) -> Iterator[Dict[str, str]]
"""
Gather details from installed distributions. Print distribution name,
version, location, and installed files. Installed files requires a
Expand All @@ -71,6 +76,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
Expand All @@ -88,7 +94,7 @@ def get_requiring_packages(package_name):
'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'):
Expand Down Expand Up @@ -141,6 +147,7 @@ def get_requiring_packages(package_name):


def print_results(distributions, list_files=False, verbose=False):
# type: (Iterator[Dict[str, str]], bool, bool) -> bool
"""
Print the information from installed distributions found.
"""
Expand Down
9 changes: 5 additions & 4 deletions src/pip/_internal/commands/wheel.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,6 +9,7 @@
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
Expand Down Expand Up @@ -50,6 +48,7 @@ class WheelCommand(RequirementCommand):
%prog [options] <archive url/path> ..."""

def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
super(WheelCommand, self).__init__(*args, **kw)

cmd_opts = self.cmd_opts
Expand Down Expand Up @@ -112,7 +111,7 @@ def __init__(self, *args, **kw):

@with_cleanup
def run(self, options, args):
# type: (Values, List[Any]) -> None
# type: (Values, List[str]) -> int
cmdoptions.check_install_build_global(options)

session = self.get_default_session(options)
Expand Down Expand Up @@ -188,3 +187,5 @@ def run(self, options, args):
raise CommandError(
"Failed to build one or more wheels"
)

return SUCCESS