1"""Uninstall-related pip exceptions."""
2
3from __future__ import annotations
4
5from typing import TYPE_CHECKING
6
7from pip._vendor.rich.text import Text
8
9from pip._internal.exceptions._base import DiagnosticPipError
10
11if TYPE_CHECKING:
12 from pip._internal.metadata import BaseDistribution
13
14
15class UninstallMissingRecord(DiagnosticPipError):
16 reference = "uninstall-no-record-file"
17
18 def __init__(self, *, distribution: BaseDistribution) -> None:
19 installer = distribution.installer
20 if not installer or installer == "pip":
21 dep = f"{distribution.raw_name}=={distribution.version}"
22 hint = Text.assemble(
23 "You might be able to recover from this via: ",
24 (f"pip install --ignore-installed --no-deps {dep}", "green"),
25 )
26 else:
27 hint = Text(
28 f"The package was installed by {installer}. "
29 "You should check if it can uninstall the package."
30 )
31
32 super().__init__(
33 message=Text(f"Cannot uninstall {distribution}"),
34 context=(
35 "The package's contents are unknown: "
36 f"no RECORD file was found for {distribution.raw_name}."
37 ),
38 hint_stmt=hint,
39 )
40
41
42class LegacyDistutilsInstall(DiagnosticPipError):
43 reference = "uninstall-distutils-installed-package"
44
45 def __init__(self, *, distribution: BaseDistribution) -> None:
46 super().__init__(
47 message=Text(f"Cannot uninstall {distribution}"),
48 context=(
49 "It is a distutils installed project and thus we cannot accurately "
50 "determine which files belong to it which would lead to only a partial "
51 "uninstall."
52 ),
53 hint_stmt=None,
54 )