Skip to content

Commit 260d4fe

Browse files
author
bmartinn
committed
mypy, type hint, pep8, py27
1 parent 47f2047 commit 260d4fe

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

src/pip/_internal/req/__init__.py

+24-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import absolute_import
55

66
import logging
7+
from functools import partial
78

89
from pip._internal.utils.logging import indent_log
910
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
@@ -15,10 +16,10 @@
1516
try:
1617
from multiprocessing.pool import Pool
1718
except ImportError: # Platform-specific: No multiprocessing available
18-
Pool = None
19+
Pool = None # type: ignore
1920

2021
if MYPY_CHECK_RUNNING:
21-
from typing import List, Optional, Sequence
22+
from typing import List, Optional, Sequence, Any
2223

2324
__all__ = [
2425
"RequirementSet", "InstallRequirement",
@@ -63,16 +64,21 @@ def install_given_reqs(
6364
)
6465

6566
# pre allocate installed package names
66-
installed = [None] * len(to_install)
67+
installed = \
68+
[None] * len(to_install) # type: List[Optional[InstallationResult]]
6769
install_args = [install_options, global_options, dict(
68-
root=root, home=home, prefix=prefix, warn_script_location=warn_script_location,
69-
use_user_site=use_user_site, pycompile=pycompile)]
70+
root=root, home=home, prefix=prefix,
71+
warn_script_location=warn_script_location,
72+
use_user_site=use_user_site, pycompile=pycompile)]
7073

7174
if Pool is not None:
7275
# first let's try to install in parallel, if we fail we do it by order.
7376
pool = Pool()
7477
try:
75-
pool_result = pool.starmap_async(__single_install, [(install_args, r) for r in to_install])
78+
pool_result = pool.map_async(partial(
79+
__single_install, install_args, allow_raise=False),
80+
to_install
81+
)
7682
# python 2.7 timeout=None will not catch KeyboardInterrupt
7783
installed = pool_result.get(timeout=999999)
7884
except (KeyboardInterrupt, SystemExit):
@@ -87,12 +93,18 @@ def install_given_reqs(
8793
with indent_log():
8894
for i, requirement in enumerate(to_install):
8995
if installed[i] is None:
90-
installed[i] = __single_install(install_args, requirement, allow_raise=True)
96+
installed[i] = __single_install(
97+
install_args, requirement, allow_raise=True)
9198

9299
return [i for i in installed if i is not None]
93100

94101

95-
def __single_install(args, a_requirement, allow_raise=False):
102+
def __single_install(
103+
args, # type: Sequence[Any]
104+
a_requirement, # type: InstallRequirement
105+
allow_raise=False, # type: bool
106+
):
107+
# type: (...) -> (Optional[InstallationResult])
96108
if a_requirement.should_reinstall:
97109
logger.info('Attempting uninstall: %s', a_requirement.name)
98110
with indent_log():
@@ -106,20 +118,16 @@ def __single_install(args, a_requirement, allow_raise=False):
106118
**args[2] # **kwargs
107119
)
108120
except Exception:
109-
should_rollback = (
110-
a_requirement.should_reinstall and
111-
not a_requirement.install_succeeded
112-
)
121+
should_rollback = (a_requirement.should_reinstall and
122+
not a_requirement.install_succeeded)
113123
# if install did not succeed, rollback previous uninstall
114124
if should_rollback:
115125
uninstalled_pathset.rollback()
116126
if allow_raise:
117127
raise
118128
else:
119-
should_commit = (
120-
a_requirement.should_reinstall and
121-
a_requirement.install_succeeded
122-
)
129+
should_commit = (a_requirement.should_reinstall and
130+
a_requirement.install_succeeded)
123131
if should_commit:
124132
uninstalled_pathset.commit()
125133
return InstallationResult(a_requirement.name)

0 commit comments

Comments
 (0)