Skip to content

Commit

Permalink
Type annotations for help, list and uninstall in commands
Browse files Browse the repository at this point in the history
  • Loading branch information
deveshks committed Apr 10, 2020
1 parent 5c9e83a commit 7901ba6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
Empty file.
10 changes: 7 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,8 @@ 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
31 changes: 25 additions & 6 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 @@ -21,6 +18,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, Dict

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

logger = logging.getLogger(__name__)

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

def __init__(self, *args, **kw):
super(ListCommand, self).__init__(*args, **kw)
# type: (List[Any], Dict[Any, Any]) -> None
# https://github.com/python/mypy/issues/4335
super(ListCommand, self).__init__(*args, **kw) # type: ignore

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,8 @@ def _build_package_finder(self, options, session):
)

def run(self, options, args):
# type: (Values, List[Any]) -> None

if options.outdated and options.uptodate:
raise CommandError(
"Options --outdated and --uptodate cannot be combined.")
Expand Down Expand Up @@ -162,24 +172,29 @@ def run(self, options, args):
self.output_package_listing(packages, options)

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}
return [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
with self._build_session(options) as session:
finder = self._build_package_finder(options, session)

Expand Down Expand Up @@ -209,6 +224,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 +243,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 +259,7 @@ def output_package_listing_columns(self, data, header):


def format_for_columns(pkgs, options):
# type: (List[Distribution], Values) -> Tuple[List[List[Any]], List[str]]
"""
Convert the package data into something usable
by output_package_listing_columns.
Expand Down Expand Up @@ -279,6 +297,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
14 changes: 10 additions & 4 deletions src/pip/_internal/commands/uninstall.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

from pip._vendor.packaging.utils import canonicalize_name
Expand All @@ -14,6 +11,11 @@
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, Dict


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

def __init__(self, *args, **kw):
super(UninstallCommand, self).__init__(*args, **kw)
# type: (List[Any], Dict[Any, Any]) -> None
# https://github.com/python/mypy/issues/4335
super(UninstallCommand, self).__init__(*args, **kw) # type: ignore
self.cmd_opts.add_option(
'-r', '--requirement',
dest='requirements',
Expand All @@ -51,6 +55,8 @@ def __init__(self, *args, **kw):
self.parser.insert_option_group(0, self.cmd_opts)

def run(self, options, args):
# type: (Values, List[Any]) -> None

session = self.get_default_session(options)

reqs_to_uninstall = {}
Expand Down

0 comments on commit 7901ba6

Please sign in to comment.