Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
pip._internal.resolution.resolvelib.resolver Namespace Reference

Data Structures

class  Resolver
 

Functions

Dict[Optional[str], int] get_topological_weights ("DirectedGraph[Optional[str]]" graph, Set[str] requirement_keys)
 
Tuple[int, str] _req_set_item_sorter (Tuple[str, InstallRequirement] item, Dict[Optional[str], int] weights)
 

Variables

 Result = RLResult[Requirement, Candidate, str]
 
 logger = logging.getLogger(__name__)
 

Function Documentation

◆ _req_set_item_sorter()

Tuple[int, str] _req_set_item_sorter ( Tuple[str, InstallRequirement item,
Dict[Optional[str], int]  weights 
)
protected
Key function used to sort install requirements for installation.

Based on the "weight" mapping calculated in ``get_installation_order()``.
The canonical package name is returned as the second member as a tie-
breaker to ensure the result is predictable, which is useful in tests.

Definition at line 288 of file resolver.py.

291) -> Tuple[int, str]:
292 """Key function used to sort install requirements for installation.
293
294 Based on the "weight" mapping calculated in ``get_installation_order()``.
295 The canonical package name is returned as the second member as a tie-
296 breaker to ensure the result is predictable, which is useful in tests.
297 """
298 name = canonicalize_name(item[0])
299 return weights[name], name

◆ get_topological_weights()

Dict[Optional[str], int] get_topological_weights ( "DirectedGraph[Optional[str]]"  graph,
Set[str]   requirement_keys 
)
Assign weights to each node based on how "deep" they are.

This implementation may change at any point in the future without prior
notice.

We first simplify the dependency graph by pruning any leaves and giving them
the highest weight: a package without any dependencies should be installed
first. This is done again and again in the same way, giving ever less weight
to the newly found leaves. The loop stops when no leaves are left: all
remaining packages have at least one dependency left in the graph.

Then we continue with the remaining graph, by taking the length for the
longest path to any node from root, ignoring any paths that contain a single
node twice (i.e. cycles). This is done through a depth-first search through
the graph, while keeping track of the path to the node.

Cycles in the graph result would result in node being revisited while also
being on its own path. In this case, take no action. This helps ensure we
don't get stuck in a cycle.

When assigning weight, the longer path (i.e. larger length) is preferred.

We are only interested in the weights of packages that are in the
requirement_keys.

Definition at line 199 of file resolver.py.

201) -> Dict[Optional[str], int]:
202 """Assign weights to each node based on how "deep" they are.
203
204 This implementation may change at any point in the future without prior
205 notice.
206
207 We first simplify the dependency graph by pruning any leaves and giving them
208 the highest weight: a package without any dependencies should be installed
209 first. This is done again and again in the same way, giving ever less weight
210 to the newly found leaves. The loop stops when no leaves are left: all
211 remaining packages have at least one dependency left in the graph.
212
213 Then we continue with the remaining graph, by taking the length for the
214 longest path to any node from root, ignoring any paths that contain a single
215 node twice (i.e. cycles). This is done through a depth-first search through
216 the graph, while keeping track of the path to the node.
217
218 Cycles in the graph result would result in node being revisited while also
219 being on its own path. In this case, take no action. This helps ensure we
220 don't get stuck in a cycle.
221
222 When assigning weight, the longer path (i.e. larger length) is preferred.
223
224 We are only interested in the weights of packages that are in the
225 requirement_keys.
226 """
227 path: Set[Optional[str]] = set()
228 weights: Dict[Optional[str], int] = {}
229
230 def visit(node: Optional[str]) -> None:
231 if node in path:
232 # We hit a cycle, so we'll break it here.
233 return
234
235 # Time to visit the children!
236 path.add(node)
237 for child in graph.iter_children(node):
238 visit(child)
239 path.remove(node)
240
241 if node not in requirement_keys:
242 return
243
244 last_known_parent_count = weights.get(node, 0)
245 weights[node] = max(last_known_parent_count, len(path))
246
247 # Simplify the graph, pruning leaves that have no dependencies.
248 # This is needed for large graphs (say over 200 packages) because the
249 # `visit` function is exponentially slower then, taking minutes.
250 # See https://github.com/pypa/pip/issues/10557
251 # We will loop until we explicitly break the loop.
252 while True:
253 leaves = set()
254 for key in graph:
255 if key is None:
256 continue
257 for _child in graph.iter_children(key):
258 # This means we have at least one child
259 break
260 else:
261 # No child.
262 leaves.add(key)
263 if not leaves:
264 # We are done simplifying.
265 break
266 # Calculate the weight for the leaves.
267 weight = len(graph) - 1
268 for leaf in leaves:
269 if leaf not in requirement_keys:
270 continue
271 weights[leaf] = weight
272 # Remove the leaves from the graph, making it simpler.
273 for leaf in leaves:
274 graph.remove(leaf)
275
276 # Visit the remaining graph.
277 # `None` is guaranteed to be the root node by resolvelib.
278 visit(None)
279
280 # Sanity check: all requirement keys should be in the weights,
281 # and no other keys should be in the weights.
282 difference = set(weights.keys()).difference(requirement_keys)
283 assert not difference, difference
284
285 return weights
286
287
for i

References i.

Referenced by Resolver.get_installation_order().

Here is the caller graph for this function:

Variable Documentation

◆ logger

logger = logging.getLogger(__name__)

Definition at line 32 of file resolver.py.

◆ Result

Result = RLResult[Requirement, Candidate, str]

Definition at line 29 of file resolver.py.