1- class AbstractProvider (object ):
1+ from __future__ import annotations
2+
3+ from typing import (
4+ TYPE_CHECKING ,
5+ Generic ,
6+ Iterable ,
7+ Iterator ,
8+ Mapping ,
9+ Sequence ,
10+ )
11+
12+ from .structs import CT , KT , RT , Matches , RequirementInformation
13+
14+ if TYPE_CHECKING :
15+ from typing import Any , Protocol
16+
17+ class Preference (Protocol ):
18+ def __lt__ (self , __other : Any ) -> bool : ...
19+
20+
21+ class AbstractProvider (Generic [RT , CT , KT ]):
222 """Delegate class to provide the required interface for the resolver."""
323
4- def identify (self , requirement_or_candidate ) :
5- """Given a requirement, return an identifier for it.
24+ def identify (self , requirement_or_candidate : RT | CT ) -> KT :
25+ """Given a requirement or candidate , return an identifier for it.
626
7- This is used to identify a requirement, e.g. whether two requirements
8- should have their specifier parts merged.
27+ This is used to identify, e.g. whether two requirements
28+ should have their specifier parts merged or a candidate matches a
29+ requirement via ``find_matches()``.
930 """
1031 raise NotImplementedError
1132
1233 def get_preference (
1334 self ,
14- identifier ,
15- resolutions ,
16- candidates ,
17- information ,
18- backtrack_causes ,
19- ):
35+ identifier : KT ,
36+ resolutions : Mapping [ KT , CT ] ,
37+ candidates : Mapping [ KT , Iterator [ CT ]] ,
38+ information : Mapping [ KT , Iterator [ RequirementInformation [ RT , CT ]]] ,
39+ backtrack_causes : Sequence [ RequirementInformation [ RT , CT ]] ,
40+ ) -> Preference :
2041 """Produce a sort key for given requirement based on preference.
2142
43+ As this is a sort key it will be called O(n) times per backtrack
44+ step, where n is the number of `identifier`s, if you have a check
45+ which is expensive in some sense. E.g. It needs to make O(n) checks
46+ per call or takes significant wall clock time, consider using
47+ `narrow_requirement_selection` to filter the `identifier`s, which
48+ is applied before this sort key is called.
49+
2250 The preference is defined as "I think this requirement should be
2351 resolved first". The lower the return value is, the more preferred
2452 this group of arguments is.
2553
2654 :param identifier: An identifier as returned by ``identify()``. This
27- identifies the dependency matches which should be returned .
55+ identifies the requirement being considered .
2856 :param resolutions: Mapping of candidates currently pinned by the
2957 resolver. Each key is an identifier, and the value is a candidate.
3058 The candidate may conflict with requirements from ``information``.
3159 :param candidates: Mapping of each dependency's possible candidates.
3260 Each value is an iterator of candidates.
3361 :param information: Mapping of requirement information of each package.
3462 Each value is an iterator of *requirement information*.
35- :param backtrack_causes: Sequence of requirement information that were
36- the requirements that caused the resolver to most recently backtrack.
63+ :param backtrack_causes: Sequence of *requirement information* that are
64+ the requirements that caused the resolver to most recently
65+ backtrack.
3766
3867 A *requirement information* instance is a named tuple with two members:
3968
@@ -60,15 +89,21 @@ def get_preference(
6089 """
6190 raise NotImplementedError
6291
63- def find_matches (self , identifier , requirements , incompatibilities ):
92+ def find_matches (
93+ self ,
94+ identifier : KT ,
95+ requirements : Mapping [KT , Iterator [RT ]],
96+ incompatibilities : Mapping [KT , Iterator [CT ]],
97+ ) -> Matches [CT ]:
6498 """Find all possible candidates that satisfy the given constraints.
6599
66- :param identifier: An identifier as returned by ``identify()``. This
67- identifies the dependency matches of which should be returned.
100+ :param identifier: An identifier as returned by ``identify()``. All
101+ candidates returned by this method should produce the same
102+ identifier.
68103 :param requirements: A mapping of requirements that all returned
69104 candidates must satisfy. Each key is an identifier, and the value
70105 an iterator of requirements for that dependency.
71- :param incompatibilities: A mapping of known incompatibilities of
106+ :param incompatibilities: A mapping of known incompatibile candidates of
72107 each dependency. Each key is an identifier, and the value an
73108 iterator of incompatibilities known to the resolver. All
74109 incompatibilities *must* be excluded from the return value.
@@ -89,7 +124,7 @@ def find_matches(self, identifier, requirements, incompatibilities):
89124 """
90125 raise NotImplementedError
91126
92- def is_satisfied_by (self , requirement , candidate ) :
127+ def is_satisfied_by (self , requirement : RT , candidate : CT ) -> bool :
93128 """Whether the given requirement can be satisfied by a candidate.
94129
95130 The candidate is guaranteed to have been generated from the
@@ -100,34 +135,62 @@ def is_satisfied_by(self, requirement, candidate):
100135 """
101136 raise NotImplementedError
102137
103- def get_dependencies (self , candidate ) :
138+ def get_dependencies (self , candidate : CT ) -> Iterable [ RT ] :
104139 """Get dependencies of a candidate.
105140
106141 This should return a collection of requirements that `candidate`
107142 specifies as its dependencies.
108143 """
109144 raise NotImplementedError
110145
146+ def narrow_requirement_selection (
147+ self ,
148+ identifiers : Iterable [KT ],
149+ resolutions : Mapping [KT , CT ],
150+ candidates : Mapping [KT , Iterator [CT ]],
151+ information : Mapping [KT , Iterator [RequirementInformation [RT , CT ]]],
152+ backtrack_causes : Sequence [RequirementInformation [RT , CT ]],
153+ ) -> Iterable [KT ]:
154+ """
155+ An optional method to narrow the selection of requirements being
156+ considered during resolution. This method is called O(1) time per
157+ backtrack step.
158+
159+ :param identifiers: An iterable of `identifiers` as returned by
160+ ``identify()``. These identify all requirements currently being
161+ considered.
162+ :param resolutions: A mapping of candidates currently pinned by the
163+ resolver. Each key is an identifier, and the value is a candidate
164+ that may conflict with requirements from ``information``.
165+ :param candidates: A mapping of each dependency's possible candidates.
166+ Each value is an iterator of candidates.
167+ :param information: A mapping of requirement information for each package.
168+ Each value is an iterator of *requirement information*.
169+ :param backtrack_causes: A sequence of *requirement information* that are
170+ the requirements causing the resolver to most recently
171+ backtrack.
111172
112- class AbstractResolver (object ):
113- """The thing that performs the actual resolution work."""
114-
115- base_exception = Exception
116-
117- def __init__ (self , provider , reporter ):
118- self .provider = provider
119- self .reporter = reporter
120-
121- def resolve (self , requirements , ** kwargs ):
122- """Take a collection of constraints, spit out the resolution result.
123-
124- This returns a representation of the final resolution state, with one
125- guarenteed attribute ``mapping`` that contains resolved candidates as
126- values. The keys are their respective identifiers.
127-
128- :param requirements: A collection of constraints.
129- :param kwargs: Additional keyword arguments that subclasses may accept.
173+ A *requirement information* instance is a named tuple with two members:
130174
131- :raises: ``self.base_exception`` or its subclass.
175+ * ``requirement`` specifies a requirement contributing to the current
176+ list of candidates.
177+ * ``parent`` specifies the candidate that provides (is depended on for)
178+ the requirement, or ``None`` to indicate a root requirement.
179+
180+ Must return a non-empty subset of `identifiers`, with the default
181+ implementation being to return `identifiers` unchanged. Those `identifiers`
182+ will then be passed to the sort key `get_preference` to pick the most
183+ prefered requirement to attempt to pin, unless `narrow_requirement_selection`
184+ returns only 1 requirement, in which case that will be used without
185+ calling the sort key `get_preference`.
186+
187+ This method is designed to be used by the provider to optimize the
188+ dependency resolution, e.g. if a check cost is O(m) and it can be done
189+ against all identifiers at once then filtering the requirement selection
190+ here will cost O(m) but making it part of the sort key in `get_preference`
191+ will cost O(m*n), where n is the number of `identifiers`.
192+
193+ Returns:
194+ Iterable[KT]: A non-empty subset of `identifiers`.
132195 """
133- raise NotImplementedError
196+ return identifiers
0 commit comments