Skip to content

Commit bba104a

Browse files
committed
Type annotations to _internal.commands.help, list
1 parent 5c9e83a commit bba104a

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

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

Whitespace-only changes.

src/pip/_internal/commands/help.py

Lines changed: 7 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, Any
10+
from optparse import Values
911

1012

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

1820
def run(self, options, args):
21+
# type: (Values, List[Any]) -> int
22+
1923
from pip._internal.commands import (
2024
commands_dict, create_command, get_similar_commands,
2125
)

src/pip/_internal/commands/list.py

Lines changed: 25 additions & 6 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
@@ -21,6 +18,14 @@
2118
write_output,
2219
)
2320
from pip._internal.utils.packaging import get_installer
21+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
22+
23+
if MYPY_CHECK_RUNNING:
24+
from optparse import Values
25+
from typing import Any, List, Set, Tuple, Dict
26+
27+
from pip._internal.network.session import PipSession
28+
from pip._vendor.pkg_resources import Distribution
2429

2530
logger = logging.getLogger(__name__)
2631

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

3843
def __init__(self, *args, **kw):
39-
super(ListCommand, self).__init__(*args, **kw)
44+
# type: (List[Any], Dict[Any, Any]) -> None
45+
# mypy from complain since the base class has a fixed set of arguments
46+
super(ListCommand, self).__init__(*args, **kw) # type: ignore
4047

4148
cmd_opts = self.cmd_opts
4249

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

135143
def run(self, options, args):
144+
# type: (Values, List[Any]) -> None
145+
136146
if options.outdated and options.uptodate:
137147
raise CommandError(
138148
"Options --outdated and --uptodate cannot be combined.")
@@ -162,24 +172,29 @@ def run(self, options, args):
162172
self.output_package_listing(packages, options)
163173

164174
def get_outdated(self, packages, options):
175+
# type: (List[Distribution], Values) -> List[Distribution]
176+
165177
return [
166178
dist for dist in self.iter_packages_latest_infos(packages, options)
167179
if dist.latest_version > dist.parsed_version
168180
]
169181

170182
def get_uptodate(self, packages, options):
183+
# type: (List[Distribution], Values) -> List[Distribution]
171184
return [
172185
dist for dist in self.iter_packages_latest_infos(packages, options)
173186
if dist.latest_version == dist.parsed_version
174187
]
175188

176189
def get_not_required(self, packages, options):
177-
dep_keys = set()
190+
# type: (List[Distribution], Values) -> List[Distribution]
191+
dep_keys = set() # type: Set[Distribution]
178192
for dist in packages:
179193
dep_keys.update(requirement.key for requirement in dist.requires())
180-
return {pkg for pkg in packages if pkg.key not in dep_keys}
194+
return [pkg for pkg in packages if pkg.key not in dep_keys]
181195

182196
def iter_packages_latest_infos(self, packages, options):
197+
# type: (List[Distribution], Values) -> Distribution
183198
with self._build_session(options) as session:
184199
finder = self._build_package_finder(options, session)
185200

@@ -209,6 +224,7 @@ def iter_packages_latest_infos(self, packages, options):
209224
yield dist
210225

211226
def output_package_listing(self, packages, options):
227+
# type: (List[Distribution], Values) -> None
212228
packages = sorted(
213229
packages,
214230
key=lambda dist: dist.project_name.lower(),
@@ -227,6 +243,7 @@ def output_package_listing(self, packages, options):
227243
write_output(format_for_json(packages, options))
228244

229245
def output_package_listing_columns(self, data, header):
246+
# type: (List[List[Any]], List[str]) -> None
230247
# insert the header first: we need to know the size of column names
231248
if len(data) > 0:
232249
data.insert(0, header)
@@ -242,6 +259,7 @@ def output_package_listing_columns(self, data, header):
242259

243260

244261
def format_for_columns(pkgs, options):
262+
# type: (List[Distribution], Values) -> Tuple[List[List[Any]], List[str]]
245263
"""
246264
Convert the package data into something usable
247265
by output_package_listing_columns.
@@ -279,6 +297,7 @@ def format_for_columns(pkgs, options):
279297

280298

281299
def format_for_json(packages, options):
300+
# type: (List[Distribution], Values) -> str
282301
data = []
283302
for dist in packages:
284303
info = {

0 commit comments

Comments
 (0)