1 from typing import FrozenSet, Iterable, Optional, Tuple, Union
3 from pip._vendor.packaging.specifiers import SpecifierSet
4 from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
5 from pip._vendor.packaging.version import LegacyVersion, Version
7 from pip._internal.models.link import Link, links_equivalent
8 from pip._internal.req.req_install import InstallRequirement
9 from pip._internal.utils.hashes import Hashes
11 CandidateLookup = Tuple[Optional["Candidate"], Optional[InstallRequirement]]
12 CandidateVersion = Union[LegacyVersion, Version]
15 def format_name(project: str, extras: FrozenSet[str]) -> str:
18 canonical_extras = sorted(canonicalize_name(e) for e in extras)
19 return "{}[{}]".format(project, ",".join(canonical_extras))
24 self, specifier: SpecifierSet, hashes: Hashes, links: FrozenSet[Link]
26 self.specifier = specifier
31 def empty(cls) -> "Constraint":
32 return Constraint(SpecifierSet(), Hashes(), frozenset())
35 def from_ireq(cls, ireq: InstallRequirement) -> "Constraint":
36 links = frozenset([ireq.link]) if ireq.link else frozenset()
37 return Constraint(ireq.specifier, ireq.hashes(trust_internet=False), links)
39 def __bool__(self) -> bool:
40 return bool(self.specifier) or bool(self.hashes) or bool(self.links)
42 def __and__(self, other: InstallRequirement) -> "Constraint":
43 if not isinstance(other, InstallRequirement):
45 specifier = self.specifier & other.specifier
46 hashes = self.hashes & other.hashes(trust_internet=False)
49 links = links.union([other.link])
50 return Constraint(specifier, hashes, links)
52 def is_satisfied_by(self, candidate: "Candidate") -> bool:
53 # Reject if there are any mismatched URL constraints on this package.
54 if self.links and not all(_match_link(link, candidate) for link in self.links):
56 # We can safely always allow prereleases here since PackageFinder
57 # already implements the prerelease logic, and would have filtered out
58 # prerelease candidates if the user does not expect them.
59 return self.specifier.contains(candidate.version, prereleases=True)
64 def project_name(self) -> NormalizedName:
65 """The "project name" of a requirement.
67 This is different from ``name`` if this requirement contains extras,
68 in which case ``name`` would contain the ``[...]`` part, while this
69 refers to the name of the project.
71 raise NotImplementedError("Subclass should override")
74 def name(self) -> str:
75 """The name identifying this requirement in the resolver.
77 This is different from ``project_name`` if this requirement contains
78 extras, where ``project_name`` would not contain the ``[...]`` part.
80 raise NotImplementedError("Subclass should override")
82 def is_satisfied_by(self, candidate: "Candidate") -> bool:
85 def get_candidate_lookup(self) -> CandidateLookup:
86 raise NotImplementedError("Subclass should override")
88 def format_for_error(self) -> str:
89 raise NotImplementedError("Subclass should override")
92 def _match_link(link: Link, candidate: "Candidate") -> bool:
93 if candidate.source_link:
94 return links_equivalent(link, candidate.source_link)
100 def project_name(self) -> NormalizedName:
101 """The "project name" of the candidate.
103 This is different from ``name`` if this candidate contains extras,
104 in which case ``name`` would contain the ``[...]`` part, while this
105 refers to the name of the project.
107 raise NotImplementedError("Override in subclass")
110 def name(self) -> str:
111 """The name identifying this candidate in the resolver.
113 This is different from ``project_name`` if this candidate contains
114 extras, where ``project_name`` would not contain the ``[...]`` part.
116 raise NotImplementedError("Override in subclass")
119 def version(self) -> CandidateVersion:
120 raise NotImplementedError("Override in subclass")
123 def is_installed(self) -> bool:
124 raise NotImplementedError("Override in subclass")
127 def is_editable(self) -> bool:
128 raise NotImplementedError("Override in subclass")
131 def source_link(self) -> Optional[Link]:
132 raise NotImplementedError("Override in subclass")
134 def iter_dependencies(self, with_requires: bool) -> Iterable[Optional[Requirement]]:
135 raise NotImplementedError("Override in subclass")
137 def get_install_requirement(self) -> Optional[InstallRequirement]:
138 raise NotImplementedError("Override in subclass")
140 def format_for_error(self) -> str:
141 raise NotImplementedError("Subclass should override")