Skip to content

Commit fe1d58b

Browse files
committed
add mypy annotations for progress_bars and spinners
1 parent b7f3c40 commit fe1d58b

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

src/pip/_internal/cli/progress_bars.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import itertools
44
import sys
5-
from signal import SIGINT, default_int_handler, signal
5+
from signal import SIGINT, default_int_handler, signal, Signals
66

77
from pip._vendor import six
88
from pip._vendor.progress.bar import Bar, FillingCirclesBar, IncrementalBar
@@ -14,7 +14,8 @@
1414
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
1515

1616
if MYPY_CHECK_RUNNING:
17-
from typing import Any
17+
from typing import Any, Dict, List, Iterator
18+
from types import FrameType
1819

1920
try:
2021
from pip._vendor import colorama
@@ -25,6 +26,7 @@
2526

2627

2728
def _select_progress_class(preferred, fallback):
29+
# type: (Bar, Bar) -> Bar
2830
encoding = getattr(preferred.file, "encoding", None)
2931

3032
# If we don't know what encoding this file is in, then we'll just assume
@@ -73,10 +75,11 @@ class InterruptibleMixin(object):
7375
"""
7476

7577
def __init__(self, *args, **kwargs):
78+
# type: (List[Any], Dict[Any, Any]) -> None
7679
"""
7780
Save the original SIGINT handler for later.
7881
"""
79-
super(InterruptibleMixin, self).__init__(*args, **kwargs)
82+
super(InterruptibleMixin, self).__init__(*args, **kwargs) # type: ignore
8083

8184
self.original_handler = signal(SIGINT, self.handle_sigint)
8285

@@ -89,29 +92,32 @@ def __init__(self, *args, **kwargs):
8992
self.original_handler = default_int_handler
9093

9194
def finish(self):
95+
# type: () -> None
9296
"""
9397
Restore the original SIGINT handler after finishing.
9498
9599
This should happen regardless of whether the progress display finishes
96100
normally, or gets interrupted.
97101
"""
98-
super(InterruptibleMixin, self).finish()
102+
super(InterruptibleMixin, self).finish() # type: ignore
99103
signal(SIGINT, self.original_handler)
100104

101105
def handle_sigint(self, signum, frame):
106+
# type: (Signals, FrameType) -> None
102107
"""
103108
Call self.finish() before delegating to the original SIGINT handler.
104109
105110
This handler should only be in place while the progress display is
106111
active.
107112
"""
108113
self.finish()
109-
self.original_handler(signum, frame)
114+
self.original_handler(signum, frame) # type: ignore
110115

111116

112117
class SilentBar(Bar):
113118

114119
def update(self):
120+
# type: () -> None
115121
pass
116122

117123

@@ -126,27 +132,31 @@ class BlueEmojiBar(IncrementalBar):
126132
class DownloadProgressMixin(object):
127133

128134
def __init__(self, *args, **kwargs):
129-
super(DownloadProgressMixin, self).__init__(*args, **kwargs)
130-
self.message = (" " * (get_indentation() + 2)) + self.message
135+
# type: (List[Any], Dict[Any, Any]) -> None
136+
super(DownloadProgressMixin, self).__init__(*args, **kwargs) # type: ignore
137+
self.message = (" " * (get_indentation() + 2)) + self.message # type: str
131138

132139
@property
133140
def downloaded(self):
134-
return format_size(self.index)
141+
# type: () -> str
142+
return format_size(self.index) # type: ignore
135143

136144
@property
137145
def download_speed(self):
146+
# type: () -> str
138147
# Avoid zero division errors...
139-
if self.avg == 0.0:
148+
if self.avg == 0.0: # type: ignore
140149
return "..."
141-
return format_size(1 / self.avg) + "/s"
150+
return format_size(1 / self.avg) + "/s" # type: ignore
142151

143152
@property
144153
def pretty_eta(self):
145-
if self.eta:
146-
return "eta {}".format(self.eta_td)
154+
# type: () -> str
155+
if self.eta: # type: ignore
156+
return "eta {}".format(self.eta_td) # type: ignore
147157
return ""
148158

149-
def iter(self, it):
159+
def iter(self, it): # type: ignore
150160
for x in it:
151161
yield x
152162
self.next(len(x))
@@ -156,22 +166,23 @@ def iter(self, it):
156166
class WindowsMixin(object):
157167

158168
def __init__(self, *args, **kwargs):
169+
# type: (List[Any], Dict[Any, Any]) -> None
159170
# The Windows terminal does not support the hide/show cursor ANSI codes
160171
# even with colorama. So we'll ensure that hide_cursor is False on
161172
# Windows.
162173
# This call needs to go before the super() call, so that hide_cursor
163174
# is set in time. The base progress bar class writes the "hide cursor"
164175
# code to the terminal in its init, so if we don't set this soon
165176
# enough, we get a "hide" with no corresponding "show"...
166-
if WINDOWS and self.hide_cursor:
177+
if WINDOWS and self.hide_cursor: # type: ignore
167178
self.hide_cursor = False
168179

169-
super(WindowsMixin, self).__init__(*args, **kwargs)
180+
super(WindowsMixin, self).__init__(*args, **kwargs) # type: ignore
170181

171182
# Check if we are running on Windows and we have the colorama module,
172183
# if we do then wrap our file with it.
173184
if WINDOWS and colorama:
174-
self.file = colorama.AnsiToWin32(self.file)
185+
self.file = colorama.AnsiToWin32(self.file) # type: ignore
175186
# The progress code expects to be able to call self.file.isatty()
176187
# but the colorama.AnsiToWin32() object doesn't have that, so we'll
177188
# add it.
@@ -223,12 +234,13 @@ class DownloadProgressSpinner(WindowsMixin, InterruptibleMixin,
223234
file = sys.stdout
224235
suffix = "%(downloaded)s %(download_speed)s"
225236

226-
def next_phase(self):
237+
def next_phase(self): # type: ignore
227238
if not hasattr(self, "_phaser"):
228239
self._phaser = itertools.cycle(self.phases)
229240
return next(self._phaser)
230241

231242
def update(self):
243+
# type: () -> None
232244
message = self.message % self
233245
phase = self.next_phase()
234246
suffix = self.suffix % self
@@ -252,7 +264,7 @@ def update(self):
252264
}
253265

254266

255-
def DownloadProgressProvider(progress_bar, max=None):
267+
def DownloadProgressProvider(progress_bar, max=None): # type: ignore
256268
if max is None or max == 0:
257269
return BAR_TYPES[progress_bar][1]().iter
258270
else:

src/pip/_internal/cli/spinners.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
1414

1515
if MYPY_CHECK_RUNNING:
16-
from typing import Iterator, IO
16+
from typing import Iterator, IO, Any
1717

1818
logger = logging.getLogger(__name__)
1919

@@ -32,6 +32,7 @@ class InteractiveSpinner(SpinnerInterface):
3232
def __init__(self, message, file=None, spin_chars="-\\|/",
3333
# Empirically, 8 updates/second looks nice
3434
min_update_interval_seconds=0.125):
35+
# type: (str, IO[str], str, float) -> None
3536
self._message = message
3637
if file is None:
3738
file = sys.stdout
@@ -45,6 +46,7 @@ def __init__(self, message, file=None, spin_chars="-\\|/",
4546
self._width = 0
4647

4748
def _write(self, status):
49+
# type: (str) -> None
4850
assert not self._finished
4951
# Erase what we wrote before by backspacing to the beginning, writing
5052
# spaces to overwrite the old text, and then backspacing again
@@ -87,6 +89,7 @@ def __init__(self, message, min_update_interval_seconds=60):
8789
self._update("started")
8890

8991
def _update(self, status):
92+
# type: (str) -> None
9093
assert not self._finished
9194
self._rate_limiter.reset()
9295
logger.info("%s: %s", self._message, status)
@@ -151,7 +154,7 @@ def open_spinner(message):
151154

152155
@contextlib.contextmanager
153156
def hidden_cursor(file):
154-
# type: (IO) -> Iterator[None]
157+
# type: (IO[str]) -> Iterator[None]
155158
# The Windows terminal does not support the hide/show cursor ANSI codes,
156159
# even via colorama. So don't even try.
157160
if WINDOWS:

0 commit comments

Comments
 (0)