1"""Utilities to lazily create and visit candidates found.
3Creating and visiting a candidate is a *very* costly operation. It involves
4fetching, extracting, potentially building modules from source, and verifying
5distribution metadata. It is therefore crucial for performance to keep
6everything here lazy all the way down, so we only touch candidates that we
7absolutely need, and not "download the world" when we only need one version of
13from typing
import TYPE_CHECKING, Any, Callable, Iterator, Optional, Set, Tuple
17from .base
import Candidate
19IndexCandidateInfo = Tuple[_BaseVersion, Callable[[], Optional[Candidate]]]
22 SequenceCandidate = Sequence[Candidate]
34 SequenceCandidate = Sequence
37def _iter_built(infos: Iterator[IndexCandidateInfo]) -> Iterator[Candidate]:
38 """Iterator for ``FoundCandidates``.
40 This iterator is used when the package is not already installed. Candidates
41 from index come later in their normal ordering.
43 versions_found: Set[_BaseVersion] = set()
44 for version, func
in infos:
45 if version
in versions_found:
55 installed: Candidate, infos: Iterator[IndexCandidateInfo]
56) -> Iterator[Candidate]:
57 """Iterator for ``FoundCandidates``.
59 This iterator is used when the resolver prefers the already-installed
60 candidate and NOT to upgrade. The installed candidate is therefore
61 always yielded first, and candidates from index come later in their
62 normal ordering, except skipped when the version is already installed.
66 for version, func
in infos:
67 if version
in versions_found:
77 installed: Candidate, infos: Iterator[IndexCandidateInfo]
78) -> Iterator[Candidate]:
79 """Iterator for ``FoundCandidates``.
81 This iterator is used when the resolver prefers to upgrade an
82 already-installed package. Candidates from index are returned in their
83 normal ordering, except replaced when the version is already installed.
85 The implementation iterates through and yields other candidates, inserting
86 the installed candidate exactly once before we start yielding older or
87 equivalent candidates, or after all other candidates if they are all newer.
89 versions_found: Set[_BaseVersion] = set()
90 for version, func
in infos:
91 if version
in versions_found:
109 """A lazy sequence to provide candidates to the resolver.
111 The intended usage is to return this from `find_matches()` so the resolver
112 can iterate through the sequence multiple times, but only access the index
113 page when remote packages are actually needed. This improve performances
114 when suitable candidates are already installed on disk.
119 get_infos: Callable[[], Iterator[IndexCandidateInfo]],
120 installed: Optional[Candidate],
121 prefers_installed: bool,
122 incompatible_ids: Set[int],
151 @functools.lru_cache(maxsize=1)
__init__(self, Callable[[], Iterator[IndexCandidateInfo]] get_infos, Optional[Candidate] installed, bool prefers_installed, Set[int] incompatible_ids)
Iterator[Candidate] __iter__(self)
Any __getitem__(self, Any index)
Iterator[Candidate] _iter_built_with_prepended(Candidate installed, Iterator[IndexCandidateInfo] infos)
Iterator[Candidate] _iter_built(Iterator[IndexCandidateInfo] infos)
Iterator[Candidate] _iter_built_with_inserted(Candidate installed, Iterator[IndexCandidateInfo] infos)