Skip to content

Commit 301f65f

Browse files
authored
Merge pull request #8016 from deveshks/add-mypy-annotations-commands
2 parents 04f63ca + 2b61137 commit 301f65f

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

news/9CD0A87D-0ACD-418E-8C02-4560A99FEB71.trivial

Whitespace-only changes.

src/pip/_internal/commands/help.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# The following comment should be removed at some point in the future.
2-
# mypy: disallow-untyped-defs=False
3-
41
from __future__ import absolute_import
52

63
from pip._internal.cli.base_command import Command
74
from pip._internal.cli.status_codes import SUCCESS
85
from pip._internal.exceptions import CommandError
6+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
7+
8+
if MYPY_CHECK_RUNNING:
9+
from typing import List
10+
from optparse import Values
911

1012

1113
class HelpCommand(Command):
@@ -16,6 +18,7 @@ class HelpCommand(Command):
1618
ignore_require_venv = True
1719

1820
def run(self, options, args):
21+
# type: (Values, List[str]) -> int
1922
from pip._internal.commands import (
2023
commands_dict, create_command, get_similar_commands,
2124
)

src/pip/_internal/commands/list.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# The following comment should be removed at some point in the future.
2-
# mypy: disallow-untyped-defs=False
3-
41
from __future__ import absolute_import
52

63
import json
@@ -10,6 +7,7 @@
107

118
from pip._internal.cli import cmdoptions
129
from pip._internal.cli.req_command import IndexGroupCommand
10+
from pip._internal.cli.status_codes import SUCCESS
1311
from pip._internal.exceptions import CommandError
1412
from pip._internal.index.package_finder import PackageFinder
1513
from pip._internal.models.selection_prefs import SelectionPreferences
@@ -21,6 +19,14 @@
2119
write_output,
2220
)
2321
from pip._internal.utils.packaging import get_installer
22+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
23+
24+
if MYPY_CHECK_RUNNING:
25+
from optparse import Values
26+
from typing import Any, List, Set, Tuple, Iterator
27+
28+
from pip._internal.network.session import PipSession
29+
from pip._vendor.pkg_resources import Distribution
2430

2531
logger = logging.getLogger(__name__)
2632

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

3844
def __init__(self, *args, **kw):
45+
# type: (*Any, **Any) -> None
3946
super(ListCommand, self).__init__(*args, **kw)
4047

4148
cmd_opts = self.cmd_opts
@@ -116,6 +123,7 @@ def __init__(self, *args, **kw):
116123
self.parser.insert_option_group(0, cmd_opts)
117124

118125
def _build_package_finder(self, options, session):
126+
# type: (Values, PipSession) -> PackageFinder
119127
"""
120128
Create a package finder appropriate to this list command.
121129
"""
@@ -133,6 +141,7 @@ def _build_package_finder(self, options, session):
133141
)
134142

135143
def run(self, options, args):
144+
# type: (Values, List[str]) -> int
136145
if options.outdated and options.uptodate:
137146
raise CommandError(
138147
"Options --outdated and --uptodate cannot be combined.")
@@ -160,30 +169,40 @@ def run(self, options, args):
160169
packages = self.get_uptodate(packages, options)
161170

162171
self.output_package_listing(packages, options)
172+
return SUCCESS
163173

164174
def get_outdated(self, packages, options):
175+
# type: (List[Distribution], Values) -> List[Distribution]
165176
return [
166177
dist for dist in self.iter_packages_latest_infos(packages, options)
167178
if dist.latest_version > dist.parsed_version
168179
]
169180

170181
def get_uptodate(self, packages, options):
182+
# type: (List[Distribution], Values) -> List[Distribution]
171183
return [
172184
dist for dist in self.iter_packages_latest_infos(packages, options)
173185
if dist.latest_version == dist.parsed_version
174186
]
175187

176188
def get_not_required(self, packages, options):
177-
dep_keys = set()
189+
# type: (List[Distribution], Values) -> List[Distribution]
190+
dep_keys = set() # type: Set[Distribution]
178191
for dist in packages:
179192
dep_keys.update(requirement.key for requirement in dist.requires())
180-
return {pkg for pkg in packages if pkg.key not in dep_keys}
193+
194+
# Create a set to remove duplicate packages, and cast it to a list
195+
# to keep the return type consistent with get_outdated and
196+
# get_uptodate
197+
return list({pkg for pkg in packages if pkg.key not in dep_keys})
181198

182199
def iter_packages_latest_infos(self, packages, options):
200+
# type: (List[Distribution], Values) -> Iterator[Distribution]
183201
with self._build_session(options) as session:
184202
finder = self._build_package_finder(options, session)
185203

186204
def latest_info(dist):
205+
# type: (Distribution) -> Distribution
187206
typ = 'unknown'
188207
all_candidates = finder.find_all_candidates(dist.key)
189208
if not options.pre:
@@ -213,6 +232,7 @@ def latest_info(dist):
213232
yield dist
214233

215234
def output_package_listing(self, packages, options):
235+
# type: (List[Distribution], Values) -> None
216236
packages = sorted(
217237
packages,
218238
key=lambda dist: dist.project_name.lower(),
@@ -231,6 +251,7 @@ def output_package_listing(self, packages, options):
231251
write_output(format_for_json(packages, options))
232252

233253
def output_package_listing_columns(self, data, header):
254+
# type: (List[List[str]], List[str]) -> None
234255
# insert the header first: we need to know the size of column names
235256
if len(data) > 0:
236257
data.insert(0, header)
@@ -246,6 +267,7 @@ def output_package_listing_columns(self, data, header):
246267

247268

248269
def format_for_columns(pkgs, options):
270+
# type: (List[Distribution], Values) -> Tuple[List[List[str]], List[str]]
249271
"""
250272
Convert the package data into something usable
251273
by output_package_listing_columns.
@@ -283,6 +305,7 @@ def format_for_columns(pkgs, options):
283305

284306

285307
def format_for_json(packages, options):
308+
# type: (List[Distribution], Values) -> str
286309
data = []
287310
for dist in packages:
288311
info = {

src/pip/_internal/commands/uninstall.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
# The following comment should be removed at some point in the future.
2-
# mypy: disallow-untyped-defs=False
3-
41
from __future__ import absolute_import
52

63
from pip._vendor.packaging.utils import canonicalize_name
74

85
from pip._internal.cli.base_command import Command
96
from pip._internal.cli.req_command import SessionCommandMixin
7+
from pip._internal.cli.status_codes import SUCCESS
108
from pip._internal.exceptions import InstallationError
119
from pip._internal.req import parse_requirements
1210
from pip._internal.req.constructors import (
1311
install_req_from_line,
1412
install_req_from_parsed_requirement,
1513
)
1614
from pip._internal.utils.misc import protect_pip_from_modification_on_windows
15+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
16+
17+
if MYPY_CHECK_RUNNING:
18+
from optparse import Values
19+
from typing import Any, List
1720

1821

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

3437
def __init__(self, *args, **kw):
38+
# type: (*Any, **Any) -> None
3539
super(UninstallCommand, self).__init__(*args, **kw)
3640
self.cmd_opts.add_option(
3741
'-r', '--requirement',
@@ -51,6 +55,7 @@ def __init__(self, *args, **kw):
5155
self.parser.insert_option_group(0, self.cmd_opts)
5256

5357
def run(self, options, args):
58+
# type: (Values, List[str]) -> int
5459
session = self.get_default_session(options)
5560

5661
reqs_to_uninstall = {}
@@ -87,3 +92,5 @@ def run(self, options, args):
8792
)
8893
if uninstall_pathset:
8994
uninstall_pathset.commit()
95+
96+
return SUCCESS

0 commit comments

Comments
 (0)