Factor out logic for getting weights
@@ -18,6 +18,7 @@ if MYPY_CHECK_RUNNING:
from typing import Dict, List, Optional, Tuple
from pip._vendor.resolvelib.resolvers import Result
+ from pip._vendor.resolvelib.structs import Graph
from pip._internal.cache import WheelCache
from pip._internal.index.package_finder import PackageFinder
@@ -124,10 +125,19 @@ class Resolver(BaseResolver):
make sure every node has a greater "weight" than all its parents.
"""
assert self._result is not None, "must call resolve() first"
- weights = {} # type: Dict[Optional[str], int]
graph = self._result.graph
- key_count = len(self._result.mapping) + 1 # Packages plus sentinal.
+ weights = get_topological_weights(graph)
+
+
+def get_topological_weights(graph):
+ # type: (Graph) -> Dict[str, int]
+ """Assign weights to each node based on how "deep" they are.
+ """
+ visited = set() # type: Set[str]
+ weights = {}
+
+ key_count = len(graph)
while len(weights) < key_count:
progressed = False
for key in graph:
@@ -151,6 +161,8 @@ class Resolver(BaseResolver):
"dependency."
)
+ return weights
+
def _req_set_item_sorter(
item, # type: Tuple[str, InstallRequirement]
