Skip to content

Commit 34073df

Browse files
committed
Merge pull request #2493 from msabramo/issue-770-redux
Fix for #770: pip install -U shouldn't look at pypi if not needed
2 parents 6023179 + 94fcd18 commit 34073df

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

pip/req/req_set.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,12 @@ def prepare_files(self, finder):
223223
if not self.ignore_installed and not req_to_install.editable:
224224
req_to_install.check_if_exists()
225225
if req_to_install.satisfied_by:
226-
if self.upgrade:
226+
# check that we don't already have an exact version match
227+
# i.e. with at least one strict req operator
228+
strict_req = set(('==', '===')) & set(
229+
op for op, _ in req_to_install.req.specs)
230+
if self.upgrade and (not strict_req or
231+
self.force_reinstall):
227232
if not (self.force_reinstall or req_to_install.link):
228233
try:
229234
link = finder.find_requirement(

tests/unit/test_req.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from mock import Mock, patch, mock_open
99
from pip.exceptions import (
1010
PreviousBuildDirError, InvalidWheelFilename, UnsupportedWheel,
11+
BestVersionAlreadyInstalled,
1112
)
1213
from pip.download import PipSession
1314
from pip.index import PackageFinder
@@ -54,6 +55,60 @@ def test_no_reuse_existing_build_dir(self, data):
5455
finder,
5556
)
5657

58+
@patch(
59+
'pip.req.req_install.pkg_resources.get_distribution',
60+
lambda x: pkg_resources.Distribution(
61+
project_name='Pygments',
62+
version='2.0.2',
63+
location='/python',
64+
)
65+
)
66+
def test_upgrade_no_look_at_pypi_if_exact_version_installed(
67+
self, data):
68+
"""
69+
If an exact version is specified for install, and that version is
70+
already installed, then there is no point going to pypi as no install
71+
is needed.
72+
"""
73+
reqset = self.basic_reqset()
74+
reqset.upgrade = True
75+
req = InstallRequirement.from_line('pygments==2.0.2')
76+
req.url = None
77+
reqset.add_requirement(req)
78+
finder = PackageFinder([data.find_links], [], session=PipSession())
79+
with patch.object(finder, 'find_requirement') as find_requirement:
80+
find_requirement.side_effect = AssertionError(
81+
'find_requirement should NOT be called')
82+
reqset.prepare_files(finder)
83+
84+
@patch(
85+
'pip.req.req_install.pkg_resources.get_distribution',
86+
lambda x: pkg_resources.Distribution(
87+
project_name='Pygments',
88+
version='2.0.2',
89+
location='/python',
90+
)
91+
)
92+
def test_upgrade_look_at_pypi_if_exact_version_installed_and_force(
93+
self, data):
94+
"""
95+
If an exact version is specified for install, and that version is
96+
already installed, but --force-reinstall was provided, we should hit
97+
PyPI.
98+
"""
99+
reqset = self.basic_reqset()
100+
reqset.upgrade = True
101+
reqset.force_reinstall = True
102+
req = InstallRequirement.from_line('pygments==2.0.2')
103+
req.url = None
104+
reqset.add_requirement(req)
105+
finder = PackageFinder([data.find_links], [], session=PipSession())
106+
with patch.object(finder, 'find_requirement') as find_requirement:
107+
find_requirement.side_effect = BestVersionAlreadyInstalled
108+
with pytest.raises(BestVersionAlreadyInstalled):
109+
reqset.prepare_files(finder)
110+
find_requirement.assert_called_once()
111+
57112
def test_environment_marker_extras(self, data):
58113
"""
59114
Test that the environment marker extras are used with

0 commit comments

Comments
 (0)