Skip to content

Commit 2de3af1

Browse files
authored
Complete type annotations in pip/_internal/commands (#10182)
1 parent f8a7439 commit 2de3af1

17 files changed

+150
-210
lines changed

src/pip/_internal/commands/__init__.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import importlib
66
from collections import OrderedDict, namedtuple
7-
from typing import Any, Optional
7+
from typing import Any, Dict, Optional
88

99
from pip._internal.cli.base_command import Command
1010

@@ -18,7 +18,7 @@
1818
# in a test-related module).
1919
# Finally, we need to pass an iterable of pairs here rather than a dict
2020
# so that the ordering won't be lost when using Python 2.7.
21-
commands_dict = OrderedDict([
21+
commands_dict: Dict[str, CommandInfo] = OrderedDict([
2222
('install', CommandInfo(
2323
'pip._internal.commands.install', 'InstallCommand',
2424
'Install packages.',
@@ -83,11 +83,10 @@
8383
'pip._internal.commands.help', 'HelpCommand',
8484
'Show help for commands.',
8585
)),
86-
]) # type: OrderedDict[str, CommandInfo]
86+
])
8787

8888

89-
def create_command(name, **kwargs):
90-
# type: (str, **Any) -> Command
89+
def create_command(name: str, **kwargs: Any) -> Command:
9190
"""
9291
Create an instance of the Command class with the given name.
9392
"""
@@ -99,8 +98,7 @@ def create_command(name, **kwargs):
9998
return command
10099

101100

102-
def get_similar_commands(name):
103-
# type: (str) -> Optional[str]
101+
def get_similar_commands(name: str) -> Optional[str]:
104102
"""Command name auto-correct."""
105103
from difflib import get_close_matches
106104

src/pip/_internal/commands/cache.py

+12-24
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ class CacheCommand(Command):
3636
%prog purge
3737
"""
3838

39-
def add_options(self):
40-
# type: () -> None
39+
def add_options(self) -> None:
4140

4241
self.cmd_opts.add_option(
4342
'--format',
@@ -50,8 +49,7 @@ def add_options(self):
5049

5150
self.parser.insert_option_group(0, self.cmd_opts)
5251

53-
def run(self, options, args):
54-
# type: (Values, List[Any]) -> int
52+
def run(self, options: Values, args: List[Any]) -> int:
5553
handlers = {
5654
"dir": self.get_cache_dir,
5755
"info": self.get_cache_info,
@@ -84,15 +82,13 @@ def run(self, options, args):
8482

8583
return SUCCESS
8684

87-
def get_cache_dir(self, options, args):
88-
# type: (Values, List[Any]) -> None
85+
def get_cache_dir(self, options: Values, args: List[Any]) -> None:
8986
if args:
9087
raise CommandError('Too many arguments')
9188

9289
logger.info(options.cache_dir)
9390

94-
def get_cache_info(self, options, args):
95-
# type: (Values, List[Any]) -> None
91+
def get_cache_info(self, options: Values, args: List[Any]) -> None:
9692
if args:
9793
raise CommandError('Too many arguments')
9894

@@ -124,8 +120,7 @@ def get_cache_info(self, options, args):
124120

125121
logger.info(message)
126122

127-
def list_cache_items(self, options, args):
128-
# type: (Values, List[Any]) -> None
123+
def list_cache_items(self, options: Values, args: List[Any]) -> None:
129124
if len(args) > 1:
130125
raise CommandError('Too many arguments')
131126

@@ -140,8 +135,7 @@ def list_cache_items(self, options, args):
140135
else:
141136
self.format_for_abspath(files)
142137

143-
def format_for_human(self, files):
144-
# type: (List[str]) -> None
138+
def format_for_human(self, files: List[str]) -> None:
145139
if not files:
146140
logger.info('Nothing cached.')
147141
return
@@ -154,8 +148,7 @@ def format_for_human(self, files):
154148
logger.info('Cache contents:\n')
155149
logger.info('\n'.join(sorted(results)))
156150

157-
def format_for_abspath(self, files):
158-
# type: (List[str]) -> None
151+
def format_for_abspath(self, files: List[str]) -> None:
159152
if not files:
160153
return
161154

@@ -165,8 +158,7 @@ def format_for_abspath(self, files):
165158

166159
logger.info('\n'.join(sorted(results)))
167160

168-
def remove_cache_items(self, options, args):
169-
# type: (Values, List[Any]) -> None
161+
def remove_cache_items(self, options: Values, args: List[Any]) -> None:
170162
if len(args) > 1:
171163
raise CommandError('Too many arguments')
172164

@@ -187,24 +179,20 @@ def remove_cache_items(self, options, args):
187179
logger.verbose("Removed %s", filename)
188180
logger.info("Files removed: %s", len(files))
189181

190-
def purge_cache(self, options, args):
191-
# type: (Values, List[Any]) -> None
182+
def purge_cache(self, options: Values, args: List[Any]) -> None:
192183
if args:
193184
raise CommandError('Too many arguments')
194185

195186
return self.remove_cache_items(options, ['*'])
196187

197-
def _cache_dir(self, options, subdir):
198-
# type: (Values, str) -> str
188+
def _cache_dir(self, options: Values, subdir: str) -> str:
199189
return os.path.join(options.cache_dir, subdir)
200190

201-
def _find_http_files(self, options):
202-
# type: (Values) -> List[str]
191+
def _find_http_files(self, options: Values) -> List[str]:
203192
http_dir = self._cache_dir(options, 'http')
204193
return filesystem.find_files(http_dir, '*')
205194

206-
def _find_wheels(self, options, pattern):
207-
# type: (Values, str) -> List[str]
195+
def _find_wheels(self, options: Values, pattern: str) -> List[str]:
208196
wheel_dir = self._cache_dir(options, 'wheels')
209197

210198
# The wheel filename format, as specified in PEP 427, is:

src/pip/_internal/commands/check.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ class CheckCommand(Command):
1919
usage = """
2020
%prog [options]"""
2121

22-
def run(self, options, args):
23-
# type: (Values, List[Any]) -> int
22+
def run(self, options: Values, args: List[Any]) -> int:
2423

2524
package_set, parsing_probs = create_package_set_from_installed()
2625
missing, conflicting = check_package_set(package_set)

src/pip/_internal/commands/completion.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ class CompletionCommand(Command):
5151

5252
ignore_require_venv = True
5353

54-
def add_options(self):
55-
# type: () -> None
54+
def add_options(self) -> None:
5655
self.cmd_opts.add_option(
5756
'--bash', '-b',
5857
action='store_const',
@@ -74,8 +73,7 @@ def add_options(self):
7473

7574
self.parser.insert_option_group(0, self.cmd_opts)
7675

77-
def run(self, options, args):
78-
# type: (Values, List[str]) -> int
76+
def run(self, options: Values, args: List[str]) -> int:
7977
"""Prints the completion code of the given shell"""
8078
shells = COMPLETION_SCRIPTS.keys()
8179
shell_options = ['--' + shell for shell in sorted(shells)]

src/pip/_internal/commands/configuration.py

+14-28
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ class ConfigurationCommand(Command):
4949
%prog [<file-option>] debug
5050
"""
5151

52-
def add_options(self):
53-
# type: () -> None
52+
def add_options(self) -> None:
5453
self.cmd_opts.add_option(
5554
'--editor',
5655
dest='editor',
@@ -88,8 +87,7 @@ def add_options(self):
8887

8988
self.parser.insert_option_group(0, self.cmd_opts)
9089

91-
def run(self, options, args):
92-
# type: (Values, List[str]) -> int
90+
def run(self, options: Values, args: List[str]) -> int:
9391
handlers = {
9492
"list": self.list_values,
9593
"edit": self.open_in_editor,
@@ -134,8 +132,7 @@ def run(self, options, args):
134132

135133
return SUCCESS
136134

137-
def _determine_file(self, options, need_value):
138-
# type: (Values, bool) -> Optional[Kind]
135+
def _determine_file(self, options: Values, need_value: bool) -> Optional[Kind]:
139136
file_options = [key for key, value in (
140137
(kinds.USER, options.user_file),
141138
(kinds.GLOBAL, options.global_file),
@@ -161,36 +158,31 @@ def _determine_file(self, options, need_value):
161158
"(--user, --site, --global) to perform."
162159
)
163160

164-
def list_values(self, options, args):
165-
# type: (Values, List[str]) -> None
161+
def list_values(self, options: Values, args: List[str]) -> None:
166162
self._get_n_args(args, "list", n=0)
167163

168164
for key, value in sorted(self.configuration.items()):
169165
write_output("%s=%r", key, value)
170166

171-
def get_name(self, options, args):
172-
# type: (Values, List[str]) -> None
167+
def get_name(self, options: Values, args: List[str]) -> None:
173168
key = self._get_n_args(args, "get [name]", n=1)
174169
value = self.configuration.get_value(key)
175170

176171
write_output("%s", value)
177172

178-
def set_name_value(self, options, args):
179-
# type: (Values, List[str]) -> None
173+
def set_name_value(self, options: Values, args: List[str]) -> None:
180174
key, value = self._get_n_args(args, "set [name] [value]", n=2)
181175
self.configuration.set_value(key, value)
182176

183177
self._save_configuration()
184178

185-
def unset_name(self, options, args):
186-
# type: (Values, List[str]) -> None
179+
def unset_name(self, options: Values, args: List[str]) -> None:
187180
key = self._get_n_args(args, "unset [name]", n=1)
188181
self.configuration.unset_value(key)
189182

190183
self._save_configuration()
191184

192-
def list_config_values(self, options, args):
193-
# type: (Values, List[str]) -> None
185+
def list_config_values(self, options: Values, args: List[str]) -> None:
194186
"""List config key-value pairs across different config files"""
195187
self._get_n_args(args, "debug", n=0)
196188

@@ -207,25 +199,22 @@ def list_config_values(self, options, args):
207199
if file_exists:
208200
self.print_config_file_values(variant)
209201

210-
def print_config_file_values(self, variant):
211-
# type: (Kind) -> None
202+
def print_config_file_values(self, variant: Kind) -> None:
212203
"""Get key-value pairs from the file of a variant"""
213204
for name, value in self.configuration.\
214205
get_values_in_config(variant).items():
215206
with indent_log():
216207
write_output("%s: %s", name, value)
217208

218-
def print_env_var_values(self):
219-
# type: () -> None
209+
def print_env_var_values(self) -> None:
220210
"""Get key-values pairs present as environment variables"""
221211
write_output("%s:", 'env_var')
222212
with indent_log():
223213
for key, value in sorted(self.configuration.get_environ_vars()):
224214
env_var = f'PIP_{key.upper()}'
225215
write_output("%s=%r", env_var, value)
226216

227-
def open_in_editor(self, options, args):
228-
# type: (Values, List[str]) -> None
217+
def open_in_editor(self, options: Values, args: List[str]) -> None:
229218
editor = self._determine_editor(options)
230219

231220
fname = self.configuration.get_file_to_edit()
@@ -240,8 +229,7 @@ def open_in_editor(self, options, args):
240229
.format(e.returncode)
241230
)
242231

243-
def _get_n_args(self, args, example, n):
244-
# type: (List[str], str, int) -> Any
232+
def _get_n_args(self, args: List[str], example: str, n: int) -> Any:
245233
"""Helper to make sure the command got the right number of arguments
246234
"""
247235
if len(args) != n:
@@ -256,8 +244,7 @@ def _get_n_args(self, args, example, n):
256244
else:
257245
return args
258246

259-
def _save_configuration(self):
260-
# type: () -> None
247+
def _save_configuration(self) -> None:
261248
# We successfully ran a modifying command. Need to save the
262249
# configuration.
263250
try:
@@ -268,8 +255,7 @@ def _save_configuration(self):
268255
)
269256
raise PipError("Internal Error.")
270257

271-
def _determine_editor(self, options):
272-
# type: (Values) -> str
258+
def _determine_editor(self, options: Values) -> str:
273259
if options.editor is not None:
274260
return options.editor
275261
elif "VISUAL" in os.environ:

0 commit comments

Comments
 (0)