Skip to content

Commit 44b3c90

Browse files
DiddiLeijauranusjr
andauthored
Complete type annotations in pip/_internal/cli
Co-authored-by: Tzu-ping Chung <[email protected]>
1 parent 9c2c52b commit 44b3c90

File tree

4 files changed

+81
-115
lines changed

4 files changed

+81
-115
lines changed

news/10065.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed all the annotations from ``pip/_internal/cli``.

src/pip/_internal/cli/progress_bars.py

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import itertools
22
import sys
33
from signal import SIGINT, default_int_handler, signal
4-
from typing import Any, Dict, List
4+
from typing import Any
55

66
from pip._vendor.progress.bar import Bar, FillingCirclesBar, IncrementalBar
77
from pip._vendor.progress.spinner import Spinner
@@ -18,8 +18,7 @@
1818
colorama = None
1919

2020

21-
def _select_progress_class(preferred, fallback):
22-
# type: (Bar, Bar) -> Bar
21+
def _select_progress_class(preferred: Bar, fallback: Bar) -> Bar:
2322
encoding = getattr(preferred.file, "encoding", None)
2423

2524
# If we don't know what encoding this file is in, then we'll just assume
@@ -67,8 +66,7 @@ class InterruptibleMixin:
6766
download has already completed, for example.
6867
"""
6968

70-
def __init__(self, *args, **kwargs):
71-
# type: (List[Any], Dict[Any, Any]) -> None
69+
def __init__(self, *args: Any, **kwargs: Any) -> None:
7270
"""
7371
Save the original SIGINT handler for later.
7472
"""
@@ -85,8 +83,7 @@ def __init__(self, *args, **kwargs):
8583
if self.original_handler is None:
8684
self.original_handler = default_int_handler
8785

88-
def finish(self):
89-
# type: () -> None
86+
def finish(self) -> None:
9087
"""
9188
Restore the original SIGINT handler after finishing.
9289
@@ -108,8 +105,7 @@ def handle_sigint(self, signum, frame): # type: ignore
108105

109106

110107
class SilentBar(Bar):
111-
def update(self):
112-
# type: () -> None
108+
def update(self) -> None:
113109
pass
114110

115111

@@ -122,28 +118,24 @@ class BlueEmojiBar(IncrementalBar):
122118

123119

124120
class DownloadProgressMixin:
125-
def __init__(self, *args, **kwargs):
126-
# type: (List[Any], Dict[Any, Any]) -> None
121+
def __init__(self, *args: Any, **kwargs: Any) -> None:
127122
# https://github.com/python/mypy/issues/5887
128123
super().__init__(*args, **kwargs) # type: ignore
129124
self.message = (" " * (get_indentation() + 2)) + self.message # type: str
130125

131126
@property
132-
def downloaded(self):
133-
# type: () -> str
127+
def downloaded(self) -> str:
134128
return format_size(self.index) # type: ignore
135129

136130
@property
137-
def download_speed(self):
138-
# type: () -> str
131+
def download_speed(self) -> str:
139132
# Avoid zero division errors...
140133
if self.avg == 0.0: # type: ignore
141134
return "..."
142135
return format_size(1 / self.avg) + "/s" # type: ignore
143136

144137
@property
145-
def pretty_eta(self):
146-
# type: () -> str
138+
def pretty_eta(self) -> str:
147139
if self.eta: # type: ignore
148140
return f"eta {self.eta_td}" # type: ignore
149141
return ""
@@ -158,8 +150,7 @@ def iter(self, it): # type: ignore
158150

159151

160152
class WindowsMixin:
161-
def __init__(self, *args, **kwargs):
162-
# type: (List[Any], Dict[Any, Any]) -> None
153+
def __init__(self, *args: Any, **kwargs: Any) -> None:
163154
# The Windows terminal does not support the hide/show cursor ANSI codes
164155
# even with colorama. So we'll ensure that hide_cursor is False on
165156
# Windows.
@@ -221,14 +212,12 @@ class DownloadProgressSpinner(
221212
file = sys.stdout
222213
suffix = "%(downloaded)s %(download_speed)s"
223214

224-
def next_phase(self):
225-
# type: () -> str
215+
def next_phase(self) -> str:
226216
if not hasattr(self, "_phaser"):
227217
self._phaser = itertools.cycle(self.phases)
228218
return next(self._phaser)
229219

230-
def update(self):
231-
# type: () -> None
220+
def update(self) -> None:
232221
message = self.message % self
233222
phase = self.next_phase()
234223
suffix = self.suffix % self

src/pip/_internal/cli/req_command.py

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@ class SessionCommandMixin(CommandContextMixIn):
5050
A class mixin for command classes needing _build_session().
5151
"""
5252

53-
def __init__(self):
54-
# type: () -> None
53+
def __init__(self) -> None:
5554
super().__init__()
56-
self._session = None # Optional[PipSession]
55+
self._session: Optional[PipSession] = None
5756

5857
@classmethod
59-
def _get_index_urls(cls, options):
60-
# type: (Values) -> Optional[List[str]]
58+
def _get_index_urls(cls, options: Values) -> Optional[List[str]]:
6159
"""Return a list of index urls from user-provided options."""
6260
index_urls = []
6361
if not getattr(options, "no_index", False):
@@ -70,8 +68,7 @@ def _get_index_urls(cls, options):
7068
# Return None rather than an empty list
7169
return index_urls or None
7270

73-
def get_default_session(self, options):
74-
# type: (Values) -> PipSession
71+
def get_default_session(self, options: Values) -> PipSession:
7572
"""Get a default-managed session."""
7673
if self._session is None:
7774
self._session = self.enter_context(self._build_session(options))
@@ -81,8 +78,12 @@ def get_default_session(self, options):
8178
assert self._session is not None
8279
return self._session
8380

84-
def _build_session(self, options, retries=None, timeout=None):
85-
# type: (Values, Optional[int], Optional[int]) -> PipSession
81+
def _build_session(
82+
self,
83+
options: Values,
84+
retries: Optional[int] = None,
85+
timeout: Optional[int] = None,
86+
) -> PipSession:
8687
assert not options.cache_dir or os.path.isabs(options.cache_dir)
8788
session = PipSession(
8889
cache=(
@@ -126,8 +127,7 @@ class IndexGroupCommand(Command, SessionCommandMixin):
126127
This also corresponds to the commands that permit the pip version check.
127128
"""
128129

129-
def handle_pip_version_check(self, options):
130-
# type: (Values) -> None
130+
def handle_pip_version_check(self, options: Values) -> None:
131131
"""
132132
Do the pip version check if not disabled.
133133
@@ -154,8 +154,7 @@ def handle_pip_version_check(self, options):
154154
]
155155

156156

157-
def warn_if_run_as_root():
158-
# type: () -> None
157+
def warn_if_run_as_root() -> None:
159158
"""Output a warning for sudo users on Unix.
160159
161160
In a virtual environment, sudo pip still writes to virtualenv.
@@ -184,19 +183,18 @@ def warn_if_run_as_root():
184183
)
185184

186185

187-
def with_cleanup(func):
188-
# type: (Any) -> Any
186+
def with_cleanup(func: Any) -> Any:
189187
"""Decorator for common logic related to managing temporary
190188
directories.
191189
"""
192190

193-
def configure_tempdir_registry(registry):
194-
# type: (TempDirectoryTypeRegistry) -> None
191+
def configure_tempdir_registry(registry: TempDirectoryTypeRegistry) -> None:
195192
for t in KEEPABLE_TEMPDIR_TYPES:
196193
registry.set_delete(t, False)
197194

198-
def wrapper(self, options, args):
199-
# type: (RequirementCommand, Values, List[Any]) -> Optional[int]
195+
def wrapper(
196+
self: RequirementCommand, options: Values, args: List[Any]
197+
) -> Optional[int]:
200198
assert self.tempdir_registry is not None
201199
if options.no_clean:
202200
configure_tempdir_registry(self.tempdir_registry)
@@ -214,15 +212,13 @@ def wrapper(self, options, args):
214212

215213

216214
class RequirementCommand(IndexGroupCommand):
217-
def __init__(self, *args, **kw):
218-
# type: (Any, Any) -> None
215+
def __init__(self, *args: Any, **kw: Any) -> None:
219216
super().__init__(*args, **kw)
220217

221218
self.cmd_opts.add_option(cmdoptions.no_clean())
222219

223220
@staticmethod
224-
def determine_resolver_variant(options):
225-
# type: (Values) -> str
221+
def determine_resolver_variant(options: Values) -> str:
226222
"""Determines which resolver should be used, based on the given options."""
227223
if "legacy-resolver" in options.deprecated_features_enabled:
228224
return "legacy"
@@ -232,15 +228,14 @@ def determine_resolver_variant(options):
232228
@classmethod
233229
def make_requirement_preparer(
234230
cls,
235-
temp_build_dir, # type: TempDirectory
236-
options, # type: Values
237-
req_tracker, # type: RequirementTracker
238-
session, # type: PipSession
239-
finder, # type: PackageFinder
240-
use_user_site, # type: bool
241-
download_dir=None, # type: str
242-
):
243-
# type: (...) -> RequirementPreparer
231+
temp_build_dir: TempDirectory,
232+
options: Values,
233+
req_tracker: RequirementTracker,
234+
session: PipSession,
235+
finder: PackageFinder,
236+
use_user_site: bool,
237+
download_dir: Optional[str] = None,
238+
) -> RequirementPreparer:
244239
"""
245240
Create a RequirementPreparer instance for the given parameters.
246241
"""
@@ -283,19 +278,18 @@ def make_requirement_preparer(
283278
@classmethod
284279
def make_resolver(
285280
cls,
286-
preparer, # type: RequirementPreparer
287-
finder, # type: PackageFinder
288-
options, # type: Values
289-
wheel_cache=None, # type: Optional[WheelCache]
290-
use_user_site=False, # type: bool
291-
ignore_installed=True, # type: bool
292-
ignore_requires_python=False, # type: bool
293-
force_reinstall=False, # type: bool
294-
upgrade_strategy="to-satisfy-only", # type: str
295-
use_pep517=None, # type: Optional[bool]
296-
py_version_info=None, # type: Optional[Tuple[int, ...]]
297-
):
298-
# type: (...) -> BaseResolver
281+
preparer: RequirementPreparer,
282+
finder: PackageFinder,
283+
options: Values,
284+
wheel_cache: Optional[WheelCache] = None,
285+
use_user_site: bool = False,
286+
ignore_installed: bool = True,
287+
ignore_requires_python: bool = False,
288+
force_reinstall: bool = False,
289+
upgrade_strategy: str = "to-satisfy-only",
290+
use_pep517: Optional[bool] = None,
291+
py_version_info: Optional[Tuple[int, ...]] = None,
292+
) -> BaseResolver:
299293
"""
300294
Create a Resolver instance for the given parameters.
301295
"""
@@ -342,12 +336,11 @@ def make_resolver(
342336

343337
def get_requirements(
344338
self,
345-
args, # type: List[str]
346-
options, # type: Values
347-
finder, # type: PackageFinder
348-
session, # type: PipSession
349-
):
350-
# type: (...) -> List[InstallRequirement]
339+
args: List[str],
340+
options: Values,
341+
finder: PackageFinder,
342+
session: PipSession,
343+
) -> List[InstallRequirement]:
351344
"""
352345
Parse command-line arguments into the corresponding requirements.
353346
"""
@@ -421,8 +414,7 @@ def get_requirements(
421414
return requirements
422415

423416
@staticmethod
424-
def trace_basic_info(finder):
425-
# type: (PackageFinder) -> None
417+
def trace_basic_info(finder: PackageFinder) -> None:
426418
"""
427419
Trace basic information about the provided objects.
428420
"""
@@ -434,12 +426,11 @@ def trace_basic_info(finder):
434426

435427
def _build_package_finder(
436428
self,
437-
options, # type: Values
438-
session, # type: PipSession
439-
target_python=None, # type: Optional[TargetPython]
440-
ignore_requires_python=None, # type: Optional[bool]
441-
):
442-
# type: (...) -> PackageFinder
429+
options: Values,
430+
session: PipSession,
431+
target_python: Optional[TargetPython] = None,
432+
ignore_requires_python: Optional[bool] = None,
433+
) -> PackageFinder:
443434
"""
444435
Create a package finder appropriate to this requirement command.
445436

0 commit comments

Comments
 (0)