Skip to content

Commit 80e11bf

Browse files
committed
Diagnostic error on pip uninstall of invalid package
1 parent 111eed1 commit 80e11bf

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/pip/_internal/exceptions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
if TYPE_CHECKING:
2323
from hashlib import _Hash
2424

25+
from pip._vendor.packaging.requirements import InvalidRequirement
2526
from pip._vendor.requests.models import Request, Response
2627

2728
from pip._internal.metadata import BaseDistribution
2829
from pip._internal.req.req_install import InstallRequirement
30+
from pip._internal.resolution.resolvelib.candidates import AlreadyInstalledCandidate
2931

3032
logger = logging.getLogger(__name__)
3133

@@ -775,3 +777,21 @@ def __init__(self, *, distribution: "BaseDistribution") -> None:
775777
),
776778
hint_stmt=None,
777779
)
780+
781+
class InvalidInstalledPackage(DiagnosticPipError):
782+
reference = "invalid-installed-package"
783+
784+
def __init__(
785+
self,
786+
*,
787+
package: "AlreadyInstalledCandidate",
788+
invalid_req_exc: "InvalidRequirement",
789+
) -> None:
790+
super().__init__(
791+
message=Text(
792+
f"Cannot uninstall {package} because it has an invalid requirement:\n"
793+
f"{invalid_req_exc.args[0]}."
794+
),
795+
context="Since pip 24.1+ invalid requirements can not be read by pip.",
796+
hint_stmt="Please use 'pip<24.1' if you need to uninstall this package.",
797+
)

src/pip/_internal/resolution/resolvelib/candidates.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pip._internal.exceptions import (
1010
HashError,
1111
InstallationSubprocessError,
12+
InvalidInstalledPackage,
1213
MetadataInconsistent,
1314
MetadataInvalid,
1415
)
@@ -398,8 +399,12 @@ def format_for_error(self) -> str:
398399
def iter_dependencies(self, with_requires: bool) -> Iterable[Optional[Requirement]]:
399400
if not with_requires:
400401
return
401-
for r in self.dist.iter_dependencies():
402-
yield from self._factory.make_requirements_from_spec(str(r), self._ireq)
402+
403+
try:
404+
for r in self.dist.iter_dependencies():
405+
yield from self._factory.make_requirements_from_spec(str(r), self._ireq)
406+
except InvalidRequirement as exc:
407+
raise InvalidInstalledPackage(package=self, invalid_req_exc=exc) from None
403408

404409
def get_install_requirement(self) -> Optional[InstallRequirement]:
405410
return None

0 commit comments

Comments
 (0)