Skip to content

Commit

Permalink
fix mypy and linter errors for spinners and progressbars
Browse files Browse the repository at this point in the history
  • Loading branch information
sinscary committed Feb 20, 2020
1 parent fe1d58b commit 30933a0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
48 changes: 27 additions & 21 deletions src/pip/_internal/cli/progress_bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import itertools
import sys
from signal import SIGINT, default_int_handler, signal, Signals
from signal import SIGINT, default_int_handler, signal

from pip._vendor import six
from pip._vendor.progress.bar import Bar, FillingCirclesBar, IncrementalBar
Expand All @@ -14,8 +14,7 @@
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from typing import Any, Dict, List, Iterator
from types import FrameType
from typing import Any, Dict, List

try:
from pip._vendor import colorama
Expand Down Expand Up @@ -79,7 +78,10 @@ def __init__(self, *args, **kwargs):
"""
Save the original SIGINT handler for later.
"""
super(InterruptibleMixin, self).__init__(*args, **kwargs) # type: ignore
super(InterruptibleMixin, self).__init__( # type: ignore
*args,
**kwargs
)

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

Expand All @@ -99,19 +101,18 @@ def finish(self):
This should happen regardless of whether the progress display finishes
normally, or gets interrupted.
"""
super(InterruptibleMixin, self).finish() # type: ignore
super(InterruptibleMixin, self).finish() # type: ignore
signal(SIGINT, self.original_handler)

def handle_sigint(self, signum, frame):
# type: (Signals, FrameType) -> None
def handle_sigint(self, signum, frame): # type: ignore
"""
Call self.finish() before delegating to the original SIGINT handler.
This handler should only be in place while the progress display is
active.
"""
self.finish()
self.original_handler(signum, frame) # type: ignore
self.original_handler(signum, frame)


class SilentBar(Bar):
Expand All @@ -133,30 +134,35 @@ class DownloadProgressMixin(object):

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

@property
def downloaded(self):
# type: () -> str
return format_size(self.index) # type: ignore
return format_size(self.index) # type: ignore

@property
def download_speed(self):
# type: () -> str
# Avoid zero division errors...
if self.avg == 0.0: # type: ignore
if self.avg == 0.0: # type: ignore
return "..."
return format_size(1 / self.avg) + "/s" # type: ignore
return format_size(1 / self.avg) + "/s" # type: ignore

@property
def pretty_eta(self):
# type: () -> str
if self.eta: # type: ignore
return "eta {}".format(self.eta_td) # type: ignore
if self.eta: # type: ignore
return "eta {}".format(self.eta_td) # type: ignore
return ""

def iter(self, it): # type: ignore
def iter(self, it): # type: ignore
for x in it:
yield x
self.next(len(x))
Expand All @@ -174,15 +180,15 @@ def __init__(self, *args, **kwargs):
# is set in time. The base progress bar class writes the "hide cursor"
# code to the terminal in its init, so if we don't set this soon
# enough, we get a "hide" with no corresponding "show"...
if WINDOWS and self.hide_cursor: # type: ignore
if WINDOWS and self.hide_cursor: # type: ignore
self.hide_cursor = False

super(WindowsMixin, self).__init__(*args, **kwargs) # type: ignore
super(WindowsMixin, self).__init__(*args, **kwargs) # type: ignore

# Check if we are running on Windows and we have the colorama module,
# if we do then wrap our file with it.
if WINDOWS and colorama:
self.file = colorama.AnsiToWin32(self.file) # type: ignore
self.file = colorama.AnsiToWin32(self.file) # type: ignore
# The progress code expects to be able to call self.file.isatty()
# but the colorama.AnsiToWin32() object doesn't have that, so we'll
# add it.
Expand Down Expand Up @@ -234,7 +240,7 @@ class DownloadProgressSpinner(WindowsMixin, InterruptibleMixin,
file = sys.stdout
suffix = "%(downloaded)s %(download_speed)s"

def next_phase(self): # type: ignore
def next_phase(self): # type: ignore
if not hasattr(self, "_phaser"):
self._phaser = itertools.cycle(self.phases)
return next(self._phaser)
Expand Down Expand Up @@ -264,7 +270,7 @@ def update(self):
}


def DownloadProgressProvider(progress_bar, max=None): # type: ignore
def DownloadProgressProvider(progress_bar, max=None): # type: ignore
if max is None or max == 0:
return BAR_TYPES[progress_bar][1]().iter
else:
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/cli/spinners.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from typing import Iterator, IO, Any
from typing import Iterator, IO

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/operations/build/wheel_legacy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os.path

from pip._internal.cli.spinners import open_spinner
from pip._internal.utils.setuptools_build import (
make_setuptools_bdist_wheel_args,
)
Expand All @@ -10,7 +11,6 @@
format_command_args,
)
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
from pip._internal.cli.spinners import open_spinner

if MYPY_CHECK_RUNNING:
from typing import List, Optional, Text
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/utils/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pip._vendor.six.moves import shlex_quote

from pip._internal.cli.spinners import open_spinner, SpinnerInterface
from pip._internal.cli.spinners import SpinnerInterface, open_spinner
from pip._internal.exceptions import InstallationError
from pip._internal.utils.compat import console_to_str, str_to_display
from pip._internal.utils.logging import subprocess_logger
Expand Down

0 comments on commit 30933a0

Please sign in to comment.