1
- # The following comment should be removed at some point in the future.
2
- # mypy: disallow-untyped-defs=False
3
-
4
1
from __future__ import absolute_import
5
2
6
3
import json
21
18
write_output ,
22
19
)
23
20
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
24
29
25
30
logger = logging .getLogger (__name__ )
26
31
@@ -36,7 +41,9 @@ class ListCommand(IndexGroupCommand):
36
41
%prog [options]"""
37
42
38
43
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
40
47
41
48
cmd_opts = self .cmd_opts
42
49
@@ -116,6 +123,7 @@ def __init__(self, *args, **kw):
116
123
self .parser .insert_option_group (0 , cmd_opts )
117
124
118
125
def _build_package_finder (self , options , session ):
126
+ # type: (Values, PipSession) -> PackageFinder
119
127
"""
120
128
Create a package finder appropriate to this list command.
121
129
"""
@@ -133,6 +141,8 @@ def _build_package_finder(self, options, session):
133
141
)
134
142
135
143
def run (self , options , args ):
144
+ # type: (Values, List[Any]) -> None
145
+
136
146
if options .outdated and options .uptodate :
137
147
raise CommandError (
138
148
"Options --outdated and --uptodate cannot be combined." )
@@ -162,24 +172,29 @@ def run(self, options, args):
162
172
self .output_package_listing (packages , options )
163
173
164
174
def get_outdated (self , packages , options ):
175
+ # type: (List[Distribution], Values) -> List[Distribution]
176
+
165
177
return [
166
178
dist for dist in self .iter_packages_latest_infos (packages , options )
167
179
if dist .latest_version > dist .parsed_version
168
180
]
169
181
170
182
def get_uptodate (self , packages , options ):
183
+ # type: (List[Distribution], Values) -> List[Distribution]
171
184
return [
172
185
dist for dist in self .iter_packages_latest_infos (packages , options )
173
186
if dist .latest_version == dist .parsed_version
174
187
]
175
188
176
189
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]
178
192
for dist in packages :
179
193
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 ]
181
195
182
196
def iter_packages_latest_infos (self , packages , options ):
197
+ # type: (List[Distribution], Values) -> Distribution
183
198
with self ._build_session (options ) as session :
184
199
finder = self ._build_package_finder (options , session )
185
200
@@ -209,6 +224,7 @@ def iter_packages_latest_infos(self, packages, options):
209
224
yield dist
210
225
211
226
def output_package_listing (self , packages , options ):
227
+ # type: (List[Distribution], Values) -> None
212
228
packages = sorted (
213
229
packages ,
214
230
key = lambda dist : dist .project_name .lower (),
@@ -227,6 +243,7 @@ def output_package_listing(self, packages, options):
227
243
write_output (format_for_json (packages , options ))
228
244
229
245
def output_package_listing_columns (self , data , header ):
246
+ # type: (List[List[Any]], List[str]) -> None
230
247
# insert the header first: we need to know the size of column names
231
248
if len (data ) > 0 :
232
249
data .insert (0 , header )
@@ -242,6 +259,7 @@ def output_package_listing_columns(self, data, header):
242
259
243
260
244
261
def format_for_columns (pkgs , options ):
262
+ # type: (List[Distribution], Values) -> Tuple[List[List[Any]], List[str]]
245
263
"""
246
264
Convert the package data into something usable
247
265
by output_package_listing_columns.
@@ -279,6 +297,7 @@ def format_for_columns(pkgs, options):
279
297
280
298
281
299
def format_for_json (packages , options ):
300
+ # type: (List[Distribution], Values) -> str
282
301
data = []
283
302
for dist in packages :
284
303
info = {
0 commit comments