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

Type annotations for help, list and uninstall in commands #8016

Merged
merged 4 commits into from
May 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.
9 changes: 6 additions & 3 deletions src/pip/_internal/commands/help.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# The following comment should be removed at some point in the future.
# mypy: disallow-untyped-defs=False

from __future__ import absolute_import

from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.exceptions import CommandError
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from typing import List, Any
from optparse import Values


class HelpCommand(Command):
Expand All @@ -16,6 +18,7 @@ class HelpCommand(Command):
ignore_require_venv = True

def run(self, options, args):
# type: (Values, List[Any]) -> int
from pip._internal.commands import (
commands_dict, create_command, get_similar_commands,
)
Expand Down
32 changes: 27 additions & 5 deletions src/pip/_internal/commands/list.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 json
Expand All @@ -10,6 +7,7 @@

from pip._internal.cli import cmdoptions
from pip._internal.cli.req_command import IndexGroupCommand
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.exceptions import CommandError
from pip._internal.index.package_finder import PackageFinder
from pip._internal.models.selection_prefs import SelectionPreferences
Expand All @@ -21,6 +19,14 @@
write_output,
)
from pip._internal.utils.packaging import get_installer
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from optparse import Values
from typing import Any, List, Set, Tuple

from pip._internal.network.session import PipSession
from pip._vendor.pkg_resources import Distribution

logger = logging.getLogger(__name__)

Expand All @@ -36,6 +42,7 @@ class ListCommand(IndexGroupCommand):
%prog [options]"""

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

cmd_opts = self.cmd_opts
Expand Down Expand Up @@ -116,6 +123,7 @@ def __init__(self, *args, **kw):
self.parser.insert_option_group(0, cmd_opts)

def _build_package_finder(self, options, session):
# type: (Values, PipSession) -> PackageFinder
"""
Create a package finder appropriate to this list command.
"""
Expand All @@ -133,6 +141,7 @@ def _build_package_finder(self, options, session):
)

def run(self, options, args):
# type: (Values, List[Any]) -> int
if options.outdated and options.uptodate:
raise CommandError(
"Options --outdated and --uptodate cannot be combined.")
Expand Down Expand Up @@ -160,26 +169,35 @@ def run(self, options, args):
packages = self.get_uptodate(packages, options)

self.output_package_listing(packages, options)
return SUCCESS

def get_outdated(self, packages, options):
# type: (List[Distribution], Values) -> List[Distribution]
return [
dist for dist in self.iter_packages_latest_infos(packages, options)
if dist.latest_version > dist.parsed_version
]

def get_uptodate(self, packages, options):
# type: (List[Distribution], Values) -> List[Distribution]
return [
dist for dist in self.iter_packages_latest_infos(packages, options)
if dist.latest_version == dist.parsed_version
]

def get_not_required(self, packages, options):
dep_keys = set()
# type: (List[Distribution], Values) -> List[Distribution]
dep_keys = set() # type: Set[Distribution]
for dist in packages:
dep_keys.update(requirement.key for requirement in dist.requires())
return {pkg for pkg in packages if pkg.key not in dep_keys}

# Create a set to remove duplicate packages, and cast it to a list
# to keep the return type consistent with get_outdated and
# get_uptodate
return list({pkg for pkg in packages if pkg.key not in dep_keys})

def iter_packages_latest_infos(self, packages, options):
# type: (List[Distribution], Values) -> Distribution
deveshks marked this conversation as resolved.
Show resolved Hide resolved
with self._build_session(options) as session:
finder = self._build_package_finder(options, session)

Expand Down Expand Up @@ -209,6 +227,7 @@ def iter_packages_latest_infos(self, packages, options):
yield dist

def output_package_listing(self, packages, options):
# type: (List[Distribution], Values) -> None
packages = sorted(
packages,
key=lambda dist: dist.project_name.lower(),
Expand All @@ -227,6 +246,7 @@ def output_package_listing(self, packages, options):
write_output(format_for_json(packages, options))

def output_package_listing_columns(self, data, header):
# type: (List[List[Any]], List[str]) -> None
# insert the header first: we need to know the size of column names
if len(data) > 0:
data.insert(0, header)
Expand All @@ -242,6 +262,7 @@ def output_package_listing_columns(self, data, header):


def format_for_columns(pkgs, options):
# type: (List[Distribution], Values) -> Tuple[List[List[str]], List[str]]
"""
Convert the package data into something usable
by output_package_listing_columns.
Expand Down Expand Up @@ -279,6 +300,7 @@ def format_for_columns(pkgs, options):


def format_for_json(packages, options):
# type: (List[Distribution], Values) -> str
data = []
for dist in packages:
info = {
Expand Down
13 changes: 10 additions & 3 deletions src/pip/_internal/commands/uninstall.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# The following comment should be removed at some point in the future.
# mypy: disallow-untyped-defs=False

from __future__ import absolute_import

from pip._vendor.packaging.utils import canonicalize_name

from pip._internal.cli.base_command import Command
from pip._internal.cli.req_command import SessionCommandMixin
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.exceptions import InstallationError
from pip._internal.req import parse_requirements
from pip._internal.req.constructors import (
install_req_from_line,
install_req_from_parsed_requirement,
)
from pip._internal.utils.misc import protect_pip_from_modification_on_windows
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from optparse import Values
from typing import Any, List


class UninstallCommand(Command, SessionCommandMixin):
Expand All @@ -32,6 +35,7 @@ class UninstallCommand(Command, SessionCommandMixin):
%prog [options] -r <requirements file> ..."""

def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
super(UninstallCommand, self).__init__(*args, **kw)
self.cmd_opts.add_option(
'-r', '--requirement',
Expand All @@ -51,6 +55,7 @@ def __init__(self, *args, **kw):
self.parser.insert_option_group(0, self.cmd_opts)

def run(self, options, args):
# type: (Values, List[Any]) -> int
session = self.get_default_session(options)

reqs_to_uninstall = {}
Expand Down Expand Up @@ -87,3 +92,5 @@ def run(self, options, args):
)
if uninstall_pathset:
uninstall_pathset.commit()

return SUCCESS