Skip to content

Commit

Permalink
Merge pull request #3768 from pypa/debt/lint
Browse files Browse the repository at this point in the history
Extract method for resolving the dist. Fixes complexity lint.
  • Loading branch information
jaraco committed Jan 16, 2023
2 parents 82eee6a + f964b01 commit ef49c0a
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,7 @@ def add(self, dist, entry=None, insert=True, replace=False):
keys2.append(dist.key)
self._added_new(dist)

# FIXME: 'WorkingSet.resolve' is too complex (11)
def resolve( # noqa: C901
def resolve(
self,
requirements,
env=None,
Expand Down Expand Up @@ -823,32 +822,9 @@ def resolve( # noqa: C901
if not req_extras.markers_pass(req, extras):
continue

dist = best.get(req.key)
if dist is None:
# Find the best distribution and add it to the map
dist = self.by_key.get(req.key)
if dist is None or (dist not in req and replace_conflicting):
ws = self
if env is None:
if dist is None:
env = Environment(self.entries)
else:
# Use an empty environment and workingset to avoid
# any further conflicts with the conflicting
# distribution
env = Environment([])
ws = WorkingSet([])
dist = best[req.key] = env.best_match(
req, ws, installer, replace_conflicting=replace_conflicting
)
if dist is None:
requirers = required_by.get(req, None)
raise DistributionNotFound(req, requirers)
to_activate.append(dist)
if dist not in req:
# Oops, the "best" so far conflicts with a dependency
dependent_req = required_by[req]
raise VersionConflict(dist, req).with_context(dependent_req)
dist = self._resolve_dist(
req, best, replace_conflicting, env, installer, required_by, to_activate
)

# push the new requirements onto the stack
new_requirements = dist.requires(req.extras)[::-1]
Expand All @@ -864,6 +840,37 @@ def resolve( # noqa: C901
# return list of distros to activate
return to_activate

def _resolve_dist(
self, req, best, replace_conflicting, env, installer, required_by, to_activate
):
dist = best.get(req.key)
if dist is None:
# Find the best distribution and add it to the map
dist = self.by_key.get(req.key)
if dist is None or (dist not in req and replace_conflicting):
ws = self
if env is None:
if dist is None:
env = Environment(self.entries)
else:
# Use an empty environment and workingset to avoid
# any further conflicts with the conflicting
# distribution
env = Environment([])
ws = WorkingSet([])
dist = best[req.key] = env.best_match(
req, ws, installer, replace_conflicting=replace_conflicting
)
if dist is None:
requirers = required_by.get(req, None)
raise DistributionNotFound(req, requirers)
to_activate.append(dist)
if dist not in req:
# Oops, the "best" so far conflicts with a dependency
dependent_req = required_by[req]
raise VersionConflict(dist, req).with_context(dependent_req)
return dist

def find_plugins(self, plugin_env, full_env=None, installer=None, fallback=True):
"""Find all activatable distributions in `plugin_env`
Expand Down

0 comments on commit ef49c0a

Please sign in to comment.