Skip to content

Commit

Permalink
Drop sdist re-installation behaviour
Browse files Browse the repository at this point in the history
This was deprecated in favour of explicitly passing `--force-reinstall`.
  • Loading branch information
pradyunsg committed Oct 4, 2021
1 parent e1dd4b4 commit abd9f17
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
1 change: 1 addition & 0 deletions news/8711.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Stop reinstalling source distributions when a package, with matching name *and* version, is already installed.
17 changes: 17 additions & 0 deletions src/pip/_internal/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ def __str__(self) -> str:
)


class SamePackageAlreadyInstalled(InstallationError):
"""Installing an sdist, when a matching name-version is already installed."""

def __init__(self, *, refused_to_install: str) -> None:
super().__init__()

self.refused_to_install = refused_to_install

def __str__(self) -> str:
return (
f"Refusing to install {self.refused_to_install}\n"
"There is already an installed package with the same name and version. "
"To install the source distribution over an existing matching "
"package, use the --force-reinstall flag."
)


class UserInstallationInvalid(InstallationError):
"""A --user install is requested on an environment without user site."""

Expand Down
19 changes: 5 additions & 14 deletions src/pip/_internal/resolution/resolvelib/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pip._vendor.resolvelib.structs import DirectedGraph

from pip._internal.cache import WheelCache
from pip._internal.exceptions import SamePackageAlreadyInstalled
from pip._internal.index.package_finder import PackageFinder
from pip._internal.operations.prepare import RequirementPreparer
from pip._internal.req.req_install import InstallRequirement
Expand All @@ -19,7 +20,6 @@
PipDebuggingReporter,
PipReporter,
)
from pip._internal.utils.deprecation import deprecated
from pip._internal.utils.filetypes import is_archive_file

from .base import Candidate, Requirement
Expand Down Expand Up @@ -141,21 +141,12 @@ def resolve(
and candidate.source_link.ext != ".zip"
)
if looks_like_sdist:
# is a local sdist -- show a deprecation warning!
reason = (
"Source distribution is being reinstalled despite an "
"installed package having the same name and version as "
"the installed package."
)
replacement = "use --force-reinstall"
deprecated(
reason=reason,
replacement=replacement,
gone_in="21.3",
issue=8711,
# is a local sdist -- disallow.
raise SamePackageAlreadyInstalled(
refused_to_install=candidate.source_link.file_path
)

# is a local sdist or path -- reinstall
# is a local path -- reinstall.
ireq.should_reinstall = True
else:
continue
Expand Down
9 changes: 5 additions & 4 deletions tests/functional/test_new_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ def test_new_resolver_check_wheel_version_normalized(
script.assert_installed(simple="0.1.0+local.1")


def test_new_resolver_does_reinstall_local_sdists(script):
def test_new_resolver_refuses_to_reinstall_local_sdists(script):
archive_path = create_basic_sdist_for_package(
script,
"pkg",
Expand All @@ -1225,10 +1225,11 @@ def test_new_resolver_does_reinstall_local_sdists(script):
"--no-cache-dir",
"--no-index",
archive_path,
expect_stderr=True,
expect_error=True,
)
assert "Installing collected packages: pkg" in result.stdout, str(result)
assert "DEPRECATION" in result.stderr, str(result)
assert "Installing collected packages: pkg" not in result.stdout, str(result)
assert archive_path in result.stderr
assert "--force-reinstall" in result.stderr
script.assert_installed(pkg="1.0")


Expand Down

0 comments on commit abd9f17

Please sign in to comment.