Skip to content

Commit

Permalink
Also handle InvalidVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
notatallshaw committed Sep 28, 2024
1 parent 968a190 commit 22cc32d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
22 changes: 14 additions & 8 deletions src/pip/_internal/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
from itertools import chain, groupby, repeat
from typing import TYPE_CHECKING, Dict, Iterator, List, Literal, Optional, Union

from pip._vendor.packaging.requirements import InvalidRequirement
from pip._vendor.packaging.version import InvalidVersion
from pip._vendor.rich.console import Console, ConsoleOptions, RenderResult
from pip._vendor.rich.markup import escape
from pip._vendor.rich.text import Text

if TYPE_CHECKING:
from hashlib import _Hash

from pip._vendor.packaging.requirements import InvalidRequirement
from pip._vendor.requests.models import Request, Response

from pip._internal.metadata import BaseDistribution
from pip._internal.req.req_install import InstallRequirement
from pip._internal.resolution.resolvelib.candidates import AlreadyInstalledCandidate

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -785,19 +785,25 @@ class InvalidInstalledPackage(DiagnosticPipError):
def __init__(
self,
*,
package: "AlreadyInstalledCandidate",
invalid_req_exc: "InvalidRequirement",
dist: "BaseDistribution",
invalid_exc: Union[InvalidRequirement, InvalidVersion],
) -> None:
installed_location = package.dist.installed_location
installed_location = dist.installed_location

if isinstance(invalid_exc, InvalidRequirement):
invalid_type = "requirement"
else:
invalid_type = "version"

super().__init__(
message=Text(
f"Cannot process installed package {package} "
f"Cannot process installed package {dist} "
+ (f"in {installed_location!r} " if installed_location else "")
+ f"because it has an invalid requirement:\n{invalid_req_exc.args[0]}"
+ f"because it has an invalid {invalid_type}:\n{invalid_exc.args[0]}"
),
context=(
"Starting with pip 24.1, packages with invalid "
"requirements can not be processed."
f"{invalid_type}s can not be processed."
),
hint_stmt=(
"To proceed this package must be uninstalled using 'pip<24.1', "
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/resolution/resolvelib/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def iter_dependencies(self, with_requires: bool) -> Iterable[Optional[Requiremen
for r in self.dist.iter_dependencies():
yield from self._factory.make_requirements_from_spec(str(r), self._ireq)
except InvalidRequirement as exc:
raise InvalidInstalledPackage(package=self, invalid_req_exc=exc) from None
raise InvalidInstalledPackage(dist=self.dist, invalid_exc=exc) from None

def get_install_requirement(self) -> Optional[InstallRequirement]:
return None
Expand Down
16 changes: 11 additions & 5 deletions src/pip/_internal/resolution/resolvelib/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
from pip._vendor.packaging.requirements import InvalidRequirement
from pip._vendor.packaging.specifiers import SpecifierSet
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
from pip._vendor.packaging.version import Version
from pip._vendor.packaging.version import InvalidVersion, Version
from pip._vendor.resolvelib import ResolutionImpossible

from pip._internal.cache import CacheEntry, WheelCache
from pip._internal.exceptions import (
DistributionNotFound,
InstallationError,
InvalidInstalledPackage,
MetadataInconsistent,
MetadataInvalid,
UnsupportedPythonVersion,
Expand Down Expand Up @@ -283,10 +284,15 @@ def _get_installed_candidate() -> Optional[Candidate]:
installed_dist = self._installed_dists[name]
except KeyError:
return None
# Don't use the installed distribution if its version does not fit
# the current dependency graph.
if not specifier.contains(installed_dist.version, prereleases=True):
return None

try:
# Don't use the installed distribution if its version
# does not fit the current dependency graph.
if not specifier.contains(installed_dist.version, prereleases=True):
return None
except InvalidVersion as e:
raise InvalidInstalledPackage(dist=installed_dist, invalid_exc=e)

candidate = self._make_candidate_from_dist(
dist=installed_dist,
extras=extras,
Expand Down

0 comments on commit 22cc32d

Please sign in to comment.